我有一个表,其中有两列带有外键约束,例如:
CREATE TABLE example
(
id integer PRIMARY KEY,
f1 integer REFERENCES example(id),
f2 integer REFERENCES example(id)
);
如果我然后执行插入:
insert into example (id, f1, f2) values (1, 2, 2);
我显然会得到一个错误,但仅限于第一个失败的约束:
ERROR: insert or update on table "example" violates foreign key constraint "example_f1_fkey"
DETAIL: Key (f1)=(2) is not present in table "example".
我的问题是:是否可以配置 postgres 以便它返回一个错误,其中包含两个 失败的键约束?
非常感谢, 本
最佳答案
Is it possible to configure postgres so it returns an error with both of the failed key constraints?
不,不是。第一次 FK 失败会中止事务,因此不会运行进一步的检查。
能够捕获所有违规行为会很有趣,但在当前版本中无法做到这一点(至少在 9.3 中是这样)。
要做到这一点,您需要能够有选择地将ERROR
级别的CHECK
约束、外键约束检查等报告更改为WARNING
s,它还设置了一个标志,强制事务在当前语句结束时中止。这在技术上可能不太难做到,但肯定会涉及 PostgreSQL 源代码的大量工作。
关于postgresql - 我可以在 postgres 中获取所有外键违规吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18377314/