Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change an index defined on a table?

I have an index defined like

add_index :users, :email, :unique => true

Is there any way to change this index to drop the UNIQUE constraint, something like 'change_index'?
Or is it the only way to drop the index and add it again without the UNIQUE constraint?

like image 532
Vignesh Avatar asked Oct 22 '25 06:10

Vignesh


2 Answers

There is no "change_index" in migrations so you can do:

remove_index :users, :email

add_index :users, :email, :unique => false

like image 145
Hugo Avatar answered Oct 24 '25 21:10

Hugo


Different index types are typically implemented in very different ways in the database you are using. A primary index is very different from any secondary index. And unique indexes are typically different from search indexes to facilitate their primary use case: to quickly determine if a value is already present in a column vs. allowing efficient searches.

As such, (depending on your DBMS) you can't change an existing index. You safest bet in any case is to drop the index and create a new one. This can be done during live operations. There is no need to shutdown neither the database nor your rails app.

like image 35
Holger Just Avatar answered Oct 24 '25 21:10

Holger Just



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!