我試圖做一個更新插入到具有表部分唯一索引PostgreSQL的部分唯一索引並更新插入
create table test (
p text not null,
q text,
r text,
txt text,
unique(p,q,r)
);
create unique index test_p_idx on test(p) where q is null and r is null;
create unique index test_pq_idx on test(p, q) where r IS NULL;
create unique index test_pr_idx on test(p, r) where q is NULL;
在平原而言,p
不爲空,而只有一個q
或r
可以爲null 。
重複插入扔約束違規預期
insert into test(p,q,r,txt) values ('p',null,null,'a'); -- violates test_p_idx
insert into test(p,q,r,txt) values ('p','q',null,'b'); -- violates test_pq_idx
insert into test(p,q,r,txt) values ('p',null, 'r','c'); -- violates test_pr_idx
然而,當我試圖使用唯一約束爲UPSERT
insert into test as u (p,q,r,txt) values ('p',null,'r','d')
on conflict (p, q, r) do update
set txt = excluded.txt
它仍然拋出約束違反
但我希望on conflict
子句t抓住它並做更新。
我在做什麼錯?我應該使用index_predicate
嗎?
index_predicate Used to allow inference of partial unique indexes. Any indexes that satisfy the predicate (which need not actually be partial indexes) can be inferred. Follows CREATE INDEX format. https://www.postgresql.org/docs/9.5/static/sql-insert.html
https://stackoverflow.com/a/46728249/330315 –
的[部分索引可能的複製,同時執行了在Postgres一個UPSERT不使用關於衝突子句QL(https://stackoverflow.com/questions/46727740/partial-index-not-used-in-on-conflict-clause-while-performing-an-upsert-in-postg) – Eelke