【发布时间】:2015-05-04 18:03:32
【问题描述】:
我正在尝试创建一个表,该表将强制两个相同类型的列的唯一组合 - 在两个方向上。例如。这将是非法的:
col1 col2
1 2
2 1
我想出了这个,但它不起作用:
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
是否可以在不使用触发器或任何高级魔法的情况下仅使用约束来编写此代码?
【问题讨论】:
标签: postgresql unique combinations