如何实现在轨道上的两列的唯一索引 [英] How to implement a unique index on two columns in rails

查看:166
本文介绍了如何实现在轨道上的两列的唯一索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,我尝试添加一个唯一索引上的两列。这些列也编入索引。所以我的问题是,如果我可以删除谁是只为一列,或者如果我必须使用所有这三个指标的指标:

  add_index订阅​​,[user_ID的]
add_index订阅​​,[CONTENT_ID]
add_index订阅​​,[user_ID的],[CONTENT_ID]:独特=>真正
 

感谢您的澄清 马库斯

解决方案

  add_index订阅​​,[user_ID的,CONTENT_ID]:独特=>真正
 

另外,如果你不需要支持的Ruby 1.8或以下,你可以更简洁写:

  add_index:订阅,[:USER_ID,:CONTENT_ID],独特的:真正的
 

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

Thanks for your clarification Markus

解决方案

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

Also, if you don't need to support Ruby 1.8 or below you can write it more concisely:

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

这篇关于如何实现在轨道上的两列的唯一索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆