PostgreSQL:交易和外键问题 [英] PostgreSQL : Transaction and foreign key problem

查看:135
本文介绍了PostgreSQL:交易和外键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表A中插入一个值,它有一个串行类型作为主键。我想使用查询的返回值作为表B的外键...但我得到这个消息:
$ b

错误:在表上插入或更新tb_midia_pessoa违反了外键约束tb_midia_pessoa_id_pessoa_fkey详细信息:键(id_pessoa)=(30)在表tb_pessoa中不存在。 )

如何在不使用
的情况下启动新的事务
- 删除我的外键constraints = O



问候!
Pedro

解决方案

您可以创建一个可延迟的FK,只需使用DEFERRABLE,也许是INITIALLY DEFERRED,这取决于您。

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


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

解决方案

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

这篇关于PostgreSQL:交易和外键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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