【发布时间】:2017-10-12 23:19:09
【问题描述】:
我需要确保表 friendships 在 requester_id 和 accepter_id 列上的双向唯一性。
Table: friendships
requester_id | accepter_id
--------------------------
1 | 2
2 | 1 <-- this should not be possible
作为一个可以考虑的解决方案,我发现了这种方法:Postgresql enforce unique two-way combination of columns
create unique index ifriendz on friendz(greatest(from_id,to_id), least(from_id,to_id));
我正在尝试编写 Rails 迁移(我知道我也可以使用纯 SQL,但我希望它干净)。
这是我重写命令的尝试。它不起作用。
class AddBidirectionalUniqueConstraintToFriendships < ActiveRecord::Migration
def change
add index :friendships, greatest[:requester_id, :accepter_id], least[:requester_id, :accepter_id], unique: true
end
end
【问题讨论】:
-
我对 Rails ActiveRecord 一无所知,但如果它与大多数其他 ORM 框架一样,那么它可能仅限于大多数 ANSI 风格的 SQL 功能。
greatest和least函数在很大程度上是特定于数据库的。 Postgres 和 MySQL 支持它,但不是所有的数据库。因此,您尝试做的事情可能是不可能的。
标签: sql ruby-on-rails