Will removing a column with a Rails migration remove indexes associated with the column

不打扰是莪最后的温柔 提交于 2019-11-30 22:18:22

问题


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)


回答1:


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




回答2:


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




回答3:


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"



回答4:


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.




回答5:


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




回答6:


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.


来源:https://stackoverflow.com/questions/7204476/will-removing-a-column-with-a-rails-migration-remove-indexes-associated-with-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!