gpt4 book ai didi

sql - 可延迟最初在 postgresql 中延迟

转载 作者:行者123 更新时间:2023-11-29 11:28:42 25 4
gpt4 key购买 nike

我在 2 个表上有一个循环外键,所以我使用 deferrable initially deferred 如下:

uni=# create table vorlesungen (vnr integer primary key, gelesenvon integer);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "vorlesungen_pkey" for table "vorlesungen"
CREATE TABLE
uni=# create table professoren (pnr integer primary key, lieblingsvo integer);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "professoren_pkey" for table "professoren"
CREATE TABLE
uni=# alter table professoren add constraint vfk foreign key (lieblingsvo) references vorlesungen (vnr) deferrable initially deferred;
ALTER TABLE
uni=# alter table vorlesungen add constraint pfk foreign key (gelesenvon) references professoren (pnr) deferrable initially deferred;
ALTER TABLE

到目前为止一切顺利。但是现在当我想插入到表中时,我得到了外键违规,尽管我指定了 deferrable initially deferred:

uni=# insert into vorlesungen values (1, 1);
ERROR: insert or update on table "vorlesungen" violates foreign key constraint "pfk"
DETAIL: Key (gelesenvon)=(1) is not present in table "professoren".

uni=# insert into professoren values (1, 1);
ERROR: insert or update on table "professoren" violates foreign key constraint "vfk"
DETAIL: Key (lieblingsvo)=(1) is not present in table "vorlesungen".

有什么问题吗?

最佳答案

您是否在插入?如果不使用 BEGIN,每个插入都是一个独立的事务,因此在每个命令的末尾强制执行外键。

关于sql - 可延迟最初在 postgresql 中延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com