Will removing a column with a Rails migration remove indexes associated with the column
Asked Answered
F

6

60

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)

Foray answered 26/8, 2011 at 12:14 Comment(2)
By the way here's an interesting post that does not necessarily directly answer my question however: keyj.wordpress.com/2009/05/28/…Foray
Update: currently, it does remove the index (Rails 4.1.7).Columba
P
27

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

Pandybat answered 26/8, 2011 at 12:22 Comment(2)
for MySQL 5.1 (at least) this does not appear to be the case. See: stackoverflow.com/a/4341928Tamathatamaulipas
In Rails 4 it does now: https://mcmap.net/q/64467/-will-removing-a-column-with-a-rails-migration-remove-indexes-associated-with-the-column (e.g.: I tried and after remove_column ..., remove_index ... throws a "Index name '...' on table '...' does not exist" <3 )Bortman
I
82

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

Interosculate answered 23/12, 2014 at 14:50 Comment(1)
Although removing column will remove its index, it won't be fully reversible. Rollback will not add the index back. In this case remove_index is neededComeon
P
27

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

Pandybat answered 26/8, 2011 at 12:22 Comment(2)
for MySQL 5.1 (at least) this does not appear to be the case. See: stackoverflow.com/a/4341928Tamathatamaulipas
In Rails 4 it does now: https://mcmap.net/q/64467/-will-removing-a-column-with-a-rails-migration-remove-indexes-associated-with-the-column (e.g.: I tried and after remove_column ..., remove_index ... throws a "Index name '...' on table '...' does not exist" <3 )Bortman
M
7

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"
Mobley answered 2/10, 2012 at 10:54 Comment(1)
remove_index by name is not reversible, so it's better to use columnMontes
A
6

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.

Abdication answered 23/4, 2015 at 15:57 Comment(0)
L
1

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.
Leukoderma answered 30/8, 2016 at 11:9 Comment(0)
S
0

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

Silicle answered 28/6, 2016 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.