Rails 3:使用迁移删除表
我有一个使用迁移创建的表,现在我想删除该表。我很确定我可以取消该迁移,但我找不到执行此操作的语法。我通过搜索 Rails DB 迁移 - 如何删除表?
但他基本上说你可以找到你需要的东西并提供了一个链接。我阅读了该链接,但没有看到任何说明如何操作的内容。我看到了它的碎片,但我不知道如何将它们组合在一起。
我在迁移中看到它有一个 self.down 方法,我真的只需要知道如何调用它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试创建一个空迁移并使用:
Try to create an empty migration and use:
您可以使用以下命令回滚上次迁移:
rake db:rollback
这将运行
self.down
方法,该方法应为drop_table :table_name
You can rollback the last migration with:
rake db:rollback
That will run the
self.down
method, which should bedrop_table :table_name
其中 n 是需要回滚的步骤数。如果您关闭 STEP,它只会回滚 1.
要迁移到特定版本,请使用:
如果您想快速应用表删除,您可以创建一个新的迁移,运行它,然后将其与您的原始迁移一起删除。不再想要了。删除表的语法是:
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:
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:
销毁模型并不是最好的方法。
相反,请在 Rails 控制台中运行以下命令:
rake db:rollback
(要访问 Rails 控制台,请在终端中键入
rails c
,如下所示 此处)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)您可以使用 rake 删除表来销毁模型:
You can remove a table using rake to destroy the model: