开发者

Rails 3: Drop a table using migration

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 Migrati开发者_JS百科on - 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.


Try to create an empty migration and use:

drop_table :table_name


You can rollback the last migration with:

rake db:rollback

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


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


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)


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

rails destroy model your_model
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜