Vla*_*lad 4 postgresql foreign-key ddl
我想让我的数据库中的所有外键都可以延迟。但是不可能改变现有的约束。所以我需要删除并再次添加每个外键。如何自动完成?
a_h*_*ame 11
您可以通过生成必要的脚本来做到这一点。
select 'alter table '||quote_ident(ns.nspname)||'.'||quote_ident(tb.relname)||
' drop constraint '||quote_ident(conname)||';'||chr(10)||
'alter table '||quote_ident(ns.nspname)||'.'||quote_ident(tb.relname)||
' add constraint '||quote_ident(conname)||' '||
pg_get_constraintdef(c.oid, true)||' deferrable initially immediate;' as ddl
from pg_constraint c
join pg_class tb on tb.oid = c.conrelid
join pg_namespace ns on ns.oid = tb.relnamespace
where ns.nspname in ('public') --<<< adjust the schema name(s) here
and c.contype = 'f';
Run Code Online (Sandbox Code Playgroud)
将上述语句的输出假脱机到一个文件中,然后运行该生成的脚本。