ruby-on-railsruby-on-rails-3migrationrollbackdrop-table

Rails DB Migration - How To Drop a Table?


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?


Solution

  • You won't always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need.

    You can find information about how to accomplish different tasks in a migration here:

    http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

    More specifically, you can see how to drop a table using the following approach:

    drop_table :table_name