PostgreSQL : Transaction and foreign key problem

匆匆过客 提交于 2019-12-23 07:49:33

问题


I'm inserting a value in table A, that has a serial type as primary key. I wanna use the returned value of the query as a foreign key of table B... but I get this message:

ERROR: insert or update on table "tb_midia_pessoa" violates foreign key constraint "tb_midia_pessoa_id_pessoa_fkey" DETAIL: Key (id_pessoa)=(30) is not present in table "tb_pessoa". )

How can I make this possible without: - starting a new Transaction - droping my foreign keys constraints =O ?

Regards! Pedro


回答1:


You can make a deferrable FK, just use DEFERRABLE and maybe INITIALLY DEFERRED, that's up to you.

http://www.postgresql.org/docs/current/static/sql-createtable.html




回答2:


The statement below allows non-deferrable constraints to be deferred until transaction committed. If you don't want to change FK definitions.

SET CONSTRAINTS ALL DEFERRED;


来源:https://stackoverflow.com/questions/3730694/postgresql-transaction-and-foreign-key-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!