Miotsu
Miotsu

Reputation: 1776

Rails Migration: indexes on a renamed table

I created a table and added an index to it. On a second migration I renamed the table. Will the index keep on working?

Upvotes: 19

Views: 6252

Answers (1)

pdobb
pdobb

Reputation: 18037

Rails 3

No, you'll need to take care of the indexes yourself since the index is based on the table name. For example:

remove_index :old_table_name, :column_name
rename_table :old_table_name, :new_table_name
add_index :new_table_name, :column_name

Rails 4+

From the Rails 4 upgrade guide:

In Rails 4.0 when a column or a table is renamed the related indexes are also renamed. If you have migrations which rename the indexes, they are no longer needed.

Upvotes: 36

Related Questions