How to implement a unique index on two columns in rails

Ruby on-RailsActiverecord

Ruby on-Rails Problem Overview


I have a table and I'm trying to add a unique index on two columns. Those columns are also indexed. So my question is if I just can remove the indexes who were just for one column or if I have to use all three indexes:

add_index "subscriptions", ["user_id"]
add_index "subscriptions", ["content_id"]
add_index "subscriptions", ["user_id"], ["content_id"], :unique => true

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

add_index :subscriptions, [:user_id, :content_id], unique: true

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkusView Question on Stackoverflow
Solution 1 - Ruby on-RailsshingaraView Answer on Stackoverflow