简体   繁体   English

如何设置rails验证列hstore键的唯一性?

[英]how to set rails validates uniqueness on column hstore key?

I have a table channels , with an hstore column credentials . 我有一个表channels ,带有hstore列credentials The credentials for each channel might be different, but I want them to be unique when populated. 每个通道的凭据可能不同,但是我希望它们在填充时是唯一的。 I have set some postgres indexes like this: 我已经设置了一些类似的postgres索引:

execute "CREATE UNIQUE INDEX channels_key_seller_id_values_unique ON channels((credentials -> 'seller_id'))"
execute "CREATE UNIQUE INDEX channels_key_auth_token_values_unique ON channels((credentials -> 'auth_token'))"

This correctly raises a hard error during @channel.save with ActiveRecord::RecordNotUnique , however I would like the familiar rails way of displaying form errors. 这在使用ActiveRecord::RecordNotUnique @ channel.save期间正确地引发了一个硬错误,但是我想要熟悉的显示表单错误的方法。 What (if any) is the syntax of using validates uniqueness on hstore keys 'auth_token' and 'seller_id' ? 在hstore键“ auth_token”和“ seller_id”上使用验证唯一性的语法是什么(如果有)?

Check this, it might help: https://gist.github.com/rf-/2322543 对此进行检查,可能会有所帮助: https : //gist.github.com/rf-/2322543

Notice that he used 'sqlite3'. 注意,他使用了“ sqlite3”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM