Question

I was looking for a way to drop a table in Rails and start fresh and came across this answer: Rails DB Migration - How To Drop a Table?

However, when I ran drop_table :examples I got the following error:

-bash: drop_table: command not found

Here's my create_examples migrate file:

def self.down
  drop_table :examples
end

Can anyone help guide me into fixing this and give me insight on what I'm doing wrong? I need to fix it because this particular migrate is preventing from doing a rake db:migrate, generating the following error:

==  CreateExamples: migrating ====================================================
-- create_table(:examples)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "examples" already exists: CREATE TABLE "examples" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime) 

Thanks! (And if I need to provide more code let me know.)

Was it helpful?

Solution

You should be dropping your old table right before creating the new version:

def self.up
    drop_table :examples
    create_table :examples do |t|
        #...
    end
end

And since you can't really reverse that drop_table, you might want to raise an exception in the rollback:

def self.down
    raise ActiveRecord::IrreversibleMigration
end

Or maybe you just want to keep your current self.down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top