0
点赞
收藏
分享

微信扫一扫

PG 禁用外键约束


  • 构造数据

create table test(id int primary key , name varchar);
insert into test values (1, 'a');

create table testb(id int , name varchar, constraint fk_id foreign key (id) references test(id) match simple on delete cascade );
insert INTO testb values(1,'a1');

  • 更新

postgres=# update test set id=2 where id=1;
ERROR: update or delete on table "test" violates foreign key constraint "fk_id" on table "testb"
DETAIL: Key (id)=(1) is still referenced from table "testb".

  • 禁用外键约束之后,可以正常更新被依赖表

set session_replication_role to replica;
postgres=# update test set id=2 where id=1;
UPDATE 1
postgres=# select * from testb;
id | name
----+------
1 | a1
(1 row)

postgres=# select * from test;
id | name
----+------
2 | a
(1 row


举报

相关推荐

0 条评论