将通过 Rails 迁移删除列并删除与该列关联的索引
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 Rails 4 开始,索引会随着列的删除而自动删除。
From Rails 4 upwards, the index removes automatically with the column removal.
不,不幸的是,您必须使用
remove_index
方法。No, unfortunately you have to remove the index manually from within your migration using the
remove_index
method.为了澄清,在迁移中删除 2 列索引的语法如下
或按名称,从我的角度来看,这是一个更糟糕的选择
To clarify, inside a migration the syntax to remove a 2 column index is the following
or by name, a worse option from my point of view
需要注意的是,如果您删除列,Rails 4会为您删除索引,但您应该指定列类型。如果没有列类型,运行
rake db:rollback
将返回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 returnI 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.如果你想删除索引,你应该使用
remove_index
,如果你使用remove_column
,它会删除索引,但你不能运行rake db:rollback。正如吉姆提到的。If you want to remove index you should use
remove_index
, if you useremove_column
it does remove the index but you can't run rake db:rollback. As Jim mentioned.在 Rails 中 > 3.2.16、删除列即删除索引。
In Rails > 3.2.16, removing the column removes the index.