为什么在 table 上禁用触发器后外键处于活动状态?

Why are foreign keys active after disabling triggers on a table?

我正在尝试刷新其他数据引用的一些数据 - 我想 运行 分类并重新加载 ms_automobile table,但是 rm_automobile table 有一个外键。

看起来 'DISABLE TRIGGER' 语句有效(运行 作为 postgres,超级用户):

mobilesurvey=# ALTER TABLE ms_automobile DISABLE TRIGGER ALL;
ALTER TABLE
mobilesurvey=# ALTER TABLE rm_automobile DISABLE TRIGGER ALL;
ALTER TABLE

但是我不能再运行分类 ms_automobile table:

mobilesurvey=# TRUNCATE TABLE ms_automobile;
ERROR:  cannot truncate a table referenced in a foreign key constraint
DETAIL:  Table "rm_automobile" references "ms_automobile".
HINT:  Truncate table "rm_automobile" at the same time, or use TRUNCATE ... CASCADE.

同样,我不想丢失 rm_automobile 数据;在 TRUNCATE 之后,我打算做一个 pg_restore,其中包括丢失的 ms_automobile 数据。

如果可能的话,我想禁用而不是删除约束 - 约束更多,并且维护 disable/enable 似乎比维护 drop/add 更不容易出错。

那么,我怎样才能真正禁用这里的外键?

在 DELETE 上(而不是在 TRUNCATE 上)禁用触发器会如您所愿。

DELETE FROM ms_automobile;

TRUNCATE 的具体实现方式不同于INSERT/UPDATE/DELETE。它不使用触发器,但在执行前检查一次参照完整性。