I'm trying to create a table that would enforce a unique combination of two columns of the same type - in both directions. E.g. this would be illegal:
我正在尝试创建一个表,该表将强制执行两个相同类型的两个列的唯一组合 - 在两个方向上。例如。这将是非法的:
col1 col2
1 2
2 1
I have come up with this, but it doesn't work:
我想出了这个,但它不起作用:
database=> \d+ friend;
Table "public.friend"
Column | Type | Modifiers | Storage | Stats target | Description
--------------+--------------------------+-----------+----------+--------------+-------------
user_id_from | text | not null | extended | |
user_id_to | text | not null | extended | |
status | text | not null | extended | |
sent | timestamp with time zone | not null | plain | |
updated | timestamp with time zone | | plain | |
Indexes:
"friend_pkey" PRIMARY KEY, btree (user_id_from, user_id_to)
"friend_user_id_to_user_id_from_key" UNIQUE CONSTRAINT, btree (user_id_to, user_id_from)
Foreign-key constraints:
"friend_status_fkey" FOREIGN KEY (status) REFERENCES friend_status(name)
"friend_user_id_from_fkey" FOREIGN KEY (user_id_from) REFERENCES user_account(login)
"friend_user_id_to_fkey" FOREIGN KEY (user_id_to) REFERENCES user_account(login)
Has OIDs: no
Is it possible to write this without triggers or any advanced magic, using constraints only?
是否可以在没有触发器或任何高级魔法的情况下使用约束来编写此内容?
2 个解决方案
#1
A variation on Neil's solution which doesn't need an extension is:
Neil解决方案不需要扩展的变体是:
create table friendz (
from_id int,
to_id int
);
create unique index ifriendz on friendz(greatest(from_id,to_id), least(from_id,to_id));
Neil's solution lets you use an arbitrary number of columns though.
Neil的解决方案允许您使用任意数量的列。
We're both relying on using expressions to build the index which is documented http://www.postgresql.org/docs/9.1/static/indexes-expressional.html
我们都依赖于使用表达式来构建索引,该索引记录在http://www.postgresql.org/docs/9.1/static/indexes-expressional.html
#2
Do you consider the intarray extension to be magic?
你认为intarray扩展是神奇的吗?
You'd need to use int keys for the users instead of text though...
您需要为用户使用int键而不是文本...
Here's a possible solution:
这是一个可能的解决方案:
create extension intarray;
create table friendz (
from_id int,
to_id int
);
create unique index on friendz ( sort( array[from_id, to_id ] ) );
insert into friendz values (1,2); -- good
insert into friendz values (2,1); -- bad
#1
A variation on Neil's solution which doesn't need an extension is:
Neil解决方案不需要扩展的变体是:
create table friendz (
from_id int,
to_id int
);
create unique index ifriendz on friendz(greatest(from_id,to_id), least(from_id,to_id));
Neil's solution lets you use an arbitrary number of columns though.
Neil的解决方案允许您使用任意数量的列。
We're both relying on using expressions to build the index which is documented http://www.postgresql.org/docs/9.1/static/indexes-expressional.html
我们都依赖于使用表达式来构建索引,该索引记录在http://www.postgresql.org/docs/9.1/static/indexes-expressional.html
#2
Do you consider the intarray extension to be magic?
你认为intarray扩展是神奇的吗?
You'd need to use int keys for the users instead of text though...
您需要为用户使用int键而不是文本...
Here's a possible solution:
这是一个可能的解决方案:
create extension intarray;
create table friendz (
from_id int,
to_id int
);
create unique index on friendz ( sort( array[from_id, to_id ] ) );
insert into friendz values (1,2); -- good
insert into friendz values (2,1); -- bad