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.
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")
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.