Rails 数据库迁移 - 如何删除表? [英] Rails DB Migration - How To Drop a Table?

查看:38
本文介绍了Rails 数据库迁移 - 如何删除表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个我认为需要的表,但现在不再打算使用它.我应该如何删除该表?

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?

我已经运行了迁移,所以该表在我的数据库中.我认为 rails generate migration 应该能够处理这个问题,但我还没有弄清楚如何.

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.

我试过了:

rails generate migration drop_tablename

但这只是生成了一个空迁移.

but that just generated an empty migration.

在 Rails 中删除表的官方"方式是什么?

What is the "official" way to drop a table in Rails?

推荐答案

您并不总是能够简单地生成迁移以获取您想要的代码.您可以创建一个空的迁移,然后用您需要的代码填充它.

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

这篇关于Rails 数据库迁移 - 如何删除表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆