How to implement a unique index on two columns in rails

点点圈 提交于 2019-12-17 17:38:32

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/4123610/how-to-implement-a-unique-index-on-two-columns-in-rails

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