将通过 Rails 迁移删除列并删除与该列关联的索引

发布于 2024-12-01 13:59:29 字数 105 浏览 3 评论 0原文

在 Rails 2 中,通过 Rails 迁移删除列也会更改/删除与该列关联的索引吗?如果没有,您还必须手动更改/删除每个索引,难道不应该自动化吗?

谢谢(来自 Rails 新手)

In Rails 2, will removing a column with a Rails migration also change/remove indexes associated with the column? If not, and instead you have to also change/remove each index manually, shouldn't it instead be automated?

Thanks (from a Rails newbie)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倒带 2024-12-08 13:59:29

从 Rails 4 开始,索引会随着列的删除而自动删除。

From Rails 4 upwards, the index removes automatically with the column removal.

不知所踪 2024-12-08 13:59:29

不,不幸的是,您必须使用 remove_index 方法。

No, unfortunately you have to remove the index manually from within your migration using the remove_index method.

绿光 2024-12-08 13:59:29

为了澄清,在迁移中删除 2 列索引的语法如下

remove_index :actions, :column => [:user_id,:action_name]

或按名称,从我的角度来看,这是一个更糟糕的选择

remove_index :actions, :name => "index_actions_on_user_id_and_action_name"

To clarify, inside a migration the syntax to remove a 2 column index is the following

remove_index :actions, :column => [:user_id,:action_name]

or by name, a worse option from my point of view

remove_index :actions, :name => "index_actions_on_user_id_and_action_name"
空心空情空意 2024-12-08 13:59:29

需要注意的是,如果您删除列,Rails 4为您删除索引,但您应该指定列类型。如果没有列类型,运行rake db:rollback将返回

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

remove_column is only reversible if given a type.

I was waiting for Dropingforeign key columns that were indexed。即使在更改块中指定 index: true 似乎也无法使列在回滚时可逆。

Just as a caution, while Rails 4 will remove the index for you if you remove the column, you should specify the column type. Without a column type, running rake db:rollback will return

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

remove_column is only reversible if given a type.

I was experimenting with dropping foreign key columns that were indexed. Even specifying index: true in the change block didn't seem to make the columns reversible on rollback.

风吹雪碎 2024-12-08 13:59:29

如果你想删除索引,你应该使用remove_index,如果你使用remove_column,它会删除索引,但你不能运行rake db:rollback。正如吉姆提到的。

remove_column is only reversible if given a type.

If you want to remove index you should use remove_index, if you use remove_column it does remove the index but you can't run rake db:rollback. As Jim mentioned.

remove_column is only reversible if given a type.
秋叶绚丽 2024-12-08 13:59:29

在 Rails 中 > 3.2.16、删除列即删除索引。

In Rails > 3.2.16, removing the column removes the index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文