Postgres 11 不强制执行外键约束?

问题描述 投票:0回答:1

即使问这个问题也让我觉得很傻,但请听我说完。

我们的 PSQL 数据库中有两个表,我们称它们为 A 和 B。这是发生的情况:

postgres=# \d A;
                              Table "public.A"
       Column        |            Type             | Collation | Nullable | Default 
---------------------+-----------------------------+-----------+----------+---------
 id                  | bigint                      |           | not null | 
 b_id                | bigint                      |           | not null | 
 
Indexes:
    "a_pkey" PRIMARY KEY, btree (id)
    "idx_a_bid" btree (b_id)
Foreign-key constraints:
    "fk_a_bid" FOREIGN KEY (b_id) REFERENCES A(id)

postgres=# select count(*) from A where b_id = 522039;
 count 
-------
    90
(1 row)
                  
postgres=# select count(*) from B where id = 522039;
 count 
-------
     0
(1 row)

这里发生了什么事?怎么可能存在从 A.b_id 到 B.id 的 FK 似乎未强制执行?据我所知,这些表上的触发器从未被禁用(因此理论上已经应用了 FK 检查)。

除了有人在过去禁用触发器并从 B 中删除行之外,还有其他解释吗?

(顺便说一下,这是 PSQL 11。)

谢谢。

postgresql foreign-keys
1个回答
0
投票

这可能是由数据操作之前的某些代码设置

SET session_replication_role = replica
引起的。该语句禁用触发器,包括外键约束检查。

© www.soinside.com 2019 - 2024. All rights reserved.