gpt4 book ai didi

postgresql - 为什么在表上禁用触发器后外键处于事件状态?

转载 作者:行者123 更新时间:2023-11-29 13:17:25 26 4
gpt4 key购买 nike

我正在尝试刷新其他数据引用的一些数据 - 我想截断并重新加载 ms_automobile 表,但 rm_automobile 表有一个外键。

看起来“DISABLE TRIGGER”语句正在运行(以 super 用户 postgres 身份运行):

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

但我无法截断 ms_automobile 表:

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 之后,我计划执行一个包含丢失的 ms_automobile 数据的 pg_restore。

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

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

最佳答案

在 DELETE 上(而不是在 TRUNCATE 上),禁用触发器的工作方式与您预期的一样。

DELETE FROM ms_automobile;

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

关于postgresql - 为什么在表上禁用触发器后外键处于事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47044927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com