I created a table that I did not need. I needed to remove the table. The actual terminology is to Drop a table. The way you do this is to create a new migration.
rails g migration DropProducts
In my case, I created a products table when I didn’t needed it. Inside the migration, I do the following.
class DropProducts < ActiveRecord::Migration
def change
drop_table :products
end
end
fin
Simple and easy! Thanks!
Great Post! Wish more devs took this approach, thanks!
Simple Approach, very easy to understand and thanks.
Thanks bro. God Bless you
Should you also delete the migration files of the tables you dropped?
Depends on when you’ve made the migration. If this is production code, then I wouldn’t delete it. But if it is just development, then you could delete the previous migration and run your migrations again.
Thanks! was looking for the syntax!
what do you mean “inside the migration”? where exactly do you put that block?
Inside the generated migration file as a result of running `rails g migration DropProducts`
I’ll use it right now, thanks by your post
The problem is: You cannot rollback this migration!
def change
drop_table :products do |t|
t.string :name
# …
end
end
This would do the trick
It’s really cool but it’s not reversible migration. It’s just the up. If you want a reversible migration, you need to specify column like that :
def change
drop_table :foos_totos, id: :uuid do |t|
t.uuid :foo_id, null: false
t.uuid :toto_id, null: false
end
end