5

I have a table that I created using the migrations, now I want to get rid of this table. I'm pretty sure I can just back out that migration, but I can't find the syntax to do that. I found this question from searching Rails DB Migration - How To Drop a Table?

but he basically says you can find what you need and provides a link. I read that link and I didn't see anything that says how to do it. I saw pieces of it, but I don't know how to put them together.

I see in the migration it has a self.down method, I really just need to know how to call that.

Community
  • 1
  • 1
Jhorra
  • 6,233
  • 21
  • 69
  • 123

5 Answers5

16

Try to create an empty migration and use:

drop_table :table_name
glarkou
  • 7,023
  • 12
  • 68
  • 118
8

You can rollback the last migration with:

rake db:rollback

That will run the self.down method, which should be drop_table :table_name

zetetic
  • 47,184
  • 10
  • 111
  • 119
  • What if it was a migration I ran last month and I've done a bunch more since then? – Jhorra Aug 18 '11 at 01:37
  • @jhorra...I believe u r better off writing a new migration file having `drop_table :table_name` in the `up` method, and `create_table :table_name` in the `down` method in that case and run the migration. This way, any other person using the same project will get the table dropped, when he uses `rake db:migrate` – rubyprince Aug 18 '11 at 01:43
  • Ok, I'll go the new migration route – Jhorra Aug 18 '11 at 01:49
3
rake db:rollback STEP=n

where n is the number of steps you need to roll back. If you leave the STEP off it just rolls back 1.

To migrate to a particular version, use:

rake db:migrate:down VERSION=20080906120000

If you want to quickly apply a table drop, you could create a new migration, run it, then delete it along with the original migration you no longer want. The syntax for dropping a table is:

drop_table :table_name
numbers1311407
  • 33,686
  • 9
  • 90
  • 92
0

Destroying the model is not the best way.

Instead, run this command in your rails console: rake db:rollback

(to access the rails console, type rails c in a terminal as shown here)

HectorJ
  • 5,814
  • 3
  • 34
  • 52
Anoob K Bava
  • 598
  • 7
  • 19
-11

You can remove a table using rake to destroy the model:

rails destroy model your_model
rixter
  • 1,237
  • 3
  • 19
  • 32
  • 3
    A Rails model is not a table, nor any other part of the database. While commanding `generate model Foo` creates a corresponding database migration for creating a table for storing instances of Foo, commanding `destroy model Foo` doesn't destroy the table. As http://railsforum.com/viewtopic.php?pid=10961#p10961 says: "Beware, though - when you generate a model, it creates a database migration. If you run 'destroy' on that model, it will delete the migration file, but not the database table. ..." – Teemu Leisti Sep 12 '12 at 13:07