我在同一个表上有两个唯一的约束,我想在该表上执行一个 upsert 语句。
是否可以在 upsert 中指定两个冲突?我看到了这个:How to upsert in Postgres on conflict on one of 2 columns?
但我的问题稍微复杂一些,因为其中一个唯一约束是另一个唯一约束的子集。 IE。
unique_constraint_1 = (col_1)
unqiue_constraint_2 = (col_1, col_2)
INSERT INTO table (col_1, col_2, col_3)
VALUES (val_1, val_2, val_3)
ON CONFLICT (what do I put here to account for both constraints?)
DO NOTHING;
谢谢!
最佳答案
根据 documentation , ON CONFLICT
默认情况下涵盖所有唯一约束。
when omitted, conflicts with all usable constraints (and unique indexes) are handled
在您的情况下,不需要两个约束,正如 Grzegorz Grabek 已经指出的那样。那是因为更严格的单列约束已经覆盖了较宽松的两列约束。
关于PostgreSQL 在一个 upsert 语句中多次发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38110413/