如何在rails中的两列上实现唯一索引 [英] How to implement a unique index on two columns in rails
本文介绍了如何在rails中的两列上实现唯一索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张表,我正在尝试在两列上添加唯一索引.这些列也被索引.所以我的问题是我是否可以删除仅用于一列的索引,或者我是否必须使用所有三个索引:
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
推荐答案
add_index :subscriptions, [:user_id, :content_id], unique: true
这篇关于如何在rails中的两列上实现唯一索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文