如何在sqlalchemy表达式语言中设置约束DEFERRED? [英] How to SET CONSTRAINTS DEFERRED in sqlalchemy expression language?

查看:143
本文介绍了如何在sqlalchemy表达式语言中设置约束DEFERRED?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,我们称为table_A,它有一个引用table_B的外键。 Table_B还有一个引用table_A的外键。要插入table_A的记录,我必须将外键约束配置为初始延迟。我试图先创建table_A,第二次创建table_B输入table_A的pk,然后最终输入table_B的pk到table_A。

I have a table we'll call table_A which has a foreign key that references table_B. Table_B also has a foreign key that references table_A. To insert a record for table_A, I have to configure the foreign key constraints to be initially deferred. I'm trying to first create table_A, second create table_B entering table_A's pk, then finally enter table_B's pk into table_A.

我正在使用sqlalchemy表达式语言(而不是orm),我在设置约束延迟时遇到麻烦。我试图简单地通过引擎的连接手动设置它,但它不工作。我在table_A上设置了外键声明为DEFERRABLE INITIALLY DEFERRED。在事务开始时,我有:

I'm using the sqlalchemy expression language (not the orm), and I am having trouble setting the constraints to deferred. I tried simply setting it manually through the engine's connection but it doesn't work. I did set the foreign key declared as DEFERRABLE INITIALLY DEFERRED on table_A. At the start of the transaction, I have:

connection.execute("begin")
connection.execute("set constraints deferred")

但是,引用table_B的table_A上的fk约束继续违反。我试图查看的表,连接和事务类,但没有发现任何明确违反事务的约束。我使用Postgresql;任何帮助将非常感激。

However the fk constraint on table_A that references table_B continues to get violated. I tried looking through the Table, Connection, and transaction classes but haven't found anything that explicitly defers the constraints for the transaction. I am using Postgresql; any help would be much appreciated.

推荐答案

感谢您的建议@a_horse_with_no_name,但仍然有一个NOT NULL约束留在列,NOT NULL约束不可延迟。

Thanks for the suggestion @a_horse_with_no_name, but there was still a NOT NULL constraint left on the column, and the 'NOT NULL' constraint is not deferrable.

这篇关于如何在sqlalchemy表达式语言中设置约束DEFERRED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆