sql - DISABLE TRIGGER ALL 是否应该禁用外键约束检查?

标签 sql postgresql foreign-keys database-trigger

我正在将数据从一个 PostgreSQL v10 表复制到另一个。目标表有几个外键约束。我很惊讶我没有得到任何错误,即使外键约束引用的表都没有任何数据。

在执行复制之前,我使用了 DISABLE TRIGGER ALL 来确保目标表上定义的触发器不会触发。令我惊讶的是,复制成功了。在 ENABLE TRIGGER ALL 之后,我尝试再添加一行,即现有行的副本。由于违反外键约束而失败。然后我执行了 DISABLE TRIGGER ALL,尝试添加新行,结果成功了。

我得出结论,在 PostgreSQL 10 中,DISABLE TRIGGER ALL 将禁用外键约束检查。这是预期的行为吗?

详情可见here .

最佳答案

是的,这是预期的。

来自 "ALTER TABLE" :

DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER

These forms configure the firing of trigger(s) belonging to the table. (...) One can disable or enable a single trigger specified by name, or all triggers on the table, or only user triggers (this option excludes internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints). Disabling or enabling internally generated constraint triggers requires superuser privileges; it should be done with caution since of course the integrity of the constraint cannot be guaranteed if the triggers are not executed. (...)

关于sql - DISABLE TRIGGER ALL 是否应该禁用外键约束检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51775738/

相关文章:

postgresql - psycopg2.OperationalError 与 openerp

postgresql - 无法删除数据库

Django:Django 管理中具有唯一外键的用户配置文件

sql - 试图删除 SQL 数据库中的重复条目会删除所有记录。什么地方出了错?

sql - ORA-01821 : date format not recognized error for ISO 8601 date with local time

mysql - 在连接表上添加默认值

mysql - 组或元素的嵌套

SQL 在排序列中找到第一个大于 X 的值

java - 尝试使用 Hibernate 和 PostgreSQL 执行存储过程时出错

mysql - 我如何在 MYSQL 6.3 工作台中启用 EER 模型?或者如何为我的 "offers"表创建外键?