I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table?
I’ve already run migrations, so the table is in my database. I figure rails generate migration
should be able to handle this, but I haven’t figured out how yet.
I’ve tried:
rails generate migration drop_tablename
but that just generated an empty migration.
What is the “official” way to drop a table in Rails?
Write your migration manually. E.g. run rails g migration DropUsers
.
As for the code of the migration I’m just gonna quote Maxwell Holder’s post Rails Migration Checklist
BAD – running rake db:migrate
and then rake db:rollback
will fail
class DropUsers < ActiveRecord::Migration
def change
drop_table :users
end
end
GOOD – reveals intent that migration should not be reversible
class DropUsers < ActiveRecord::Migration
def up
drop_table :users
end
def down
fail ActiveRecord::IrreversibleMigration
end
end
BETTER – is actually reversible
class DropUsers < ActiveRecord::Migration
def change
drop_table :users do |t|
t.string :email, null: false
t.timestamps null: false
end
end
end