为什么我的常规外键约束不可延迟?
Why is my regular foreign key constraint not deferrable?
我有 2 个带有外键约束的简单表:
CREATE TABLE a(i integer);
ALTER TABLE a ADD CONSTRAINT pkey_a PRIMARY KEY (i);
CREATE TABLE b(j integer);
ALTER TABLE b add CONSTRAINT fkey_ij FOREIGN KEY (j) REFERENCES a (i);
我想在交易期间推迟约束
START TRANSACTION;
SET CONSTRAINTS fkey_ij DEFERRED;
我收到以下错误:
[42809] ERROR: constraint "fkey_ij" is not deferrable
我正在阅读 postgres SET CONSTRAINTS
docs 但不明白为什么此约束不符合延迟条件:
Currently, only UNIQUE, PRIMARY KEY, REFERENCES (foreign key), and EXCLUDE constraints are affected by this setting. NOT NULL and CHECK constraints are always checked immediately when a row is inserted or modified (not at the end of the statement). Uniqueness and exclusion constraints that have not been declared DEFERRABLE are also checked immediately.
我是否遗漏了文档中的某些内容?
A) 文档 link 适用于 9.1 版,它已经过了 EOL(~5.25 年)。
B) 是的,您错过了下一段:
Upon creation, a constraint is given one of three characteristics: DEFERRABLE INITIALLY DEFERRED, DEFERRABLE INITIALLY IMMEDIATE, or NOT DEFERRABLE. .
因此需要将约束创建为 DEFERRABLE 或对其进行更改。见 ALTER TABLE:
ALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
我有 2 个带有外键约束的简单表:
CREATE TABLE a(i integer);
ALTER TABLE a ADD CONSTRAINT pkey_a PRIMARY KEY (i);
CREATE TABLE b(j integer);
ALTER TABLE b add CONSTRAINT fkey_ij FOREIGN KEY (j) REFERENCES a (i);
我想在交易期间推迟约束
START TRANSACTION;
SET CONSTRAINTS fkey_ij DEFERRED;
我收到以下错误:
[42809] ERROR: constraint "fkey_ij" is not deferrable
我正在阅读 postgres SET CONSTRAINTS
docs 但不明白为什么此约束不符合延迟条件:
Currently, only UNIQUE, PRIMARY KEY, REFERENCES (foreign key), and EXCLUDE constraints are affected by this setting. NOT NULL and CHECK constraints are always checked immediately when a row is inserted or modified (not at the end of the statement). Uniqueness and exclusion constraints that have not been declared DEFERRABLE are also checked immediately.
我是否遗漏了文档中的某些内容?
A) 文档 link 适用于 9.1 版,它已经过了 EOL(~5.25 年)。
B) 是的,您错过了下一段:
Upon creation, a constraint is given one of three characteristics: DEFERRABLE INITIALLY DEFERRED, DEFERRABLE INITIALLY IMMEDIATE, or NOT DEFERRABLE. .
因此需要将约束创建为 DEFERRABLE 或对其进行更改。见 ALTER TABLE:
ALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]