我无法使用Postgres中的where子句创建唯一约束。例如:
CREATE UNIQUE INDEX inbox_a_constraint ON inbox (sender_id, receiver_id, src_type, src_id)
WHERE src_type IN (0, 1) AND src_id IS NOT NULL;
创建时没有错误,但是当我签入管理员时,它表明唯一性和属性被遗漏了,仅创建了一个基本的复合索引。
当我尝试在冲突中使用时,请在此表上更新为
INSERT INTO inbox
(sender_id, receiver_id, src_type, src_id, timestamp)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (sender_id, receiver_id, src_type, src_id) DO UPDATE
SET timestamp = $6
RETURNING *
我收到一个错误,说there is no unique or exclusion constraint matching the ON CONFLICT specification
编辑:有了select pg_get_indexdef('inbox_a_constraint'::regclass);
,我得到CREATE UNIQUE INDEX inbox_a_constraint ON public.inbox USING btree (sender_id, receiver_id, src_type, src_id) WHERE ((inbox_type = ANY (ARRAY[0, 1])) AND (src_id IS NOT NULL))
。因此,看起来索引定义已正确保存。但是在发生冲突时,错误仍然是一个问题。