Ruby-on-rails 如何在rails中的两列上实现唯一索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:35:40 来源:igfitidea点击:
How to implement a unique index on two columns in rails
提问by Markus
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
回答by shingara
add_index :subscriptions, [:user_id, :content_id], unique: true

