in Programming, Uncategorized

Rails: Remove a table

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

Write a Comment

Comment

12 Comments

    • 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.

  1. 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

  2. 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