1 個回答
- 最新
- 最多得票
- 最多評論
0
【以下的回答经过翻译处理】 找到了解决方案。解决方案是在完整加载前手动删除外键约束,并在迁移任务结束后重新创建它们。
一个简单的脚本用于删除外键约束(摘自 https://dba.stackexchange.com/a/97047)
create table if not exists dropped_foreign_keys (
seq bigserial primary key,
sql text
);
do $$ declare t record;
begin
for t in select conrelid::regclass::varchar table_name, conname constraint_name,
pg_catalog.pg_get_constraintdef(r.oid, true) constraint_definition
from pg_catalog.pg_constraint r
where r.contype = 'f'
-- uncomment the below line for current schema only:
-- and r.connamespace = (select n.oid from pg_namespace n where n.nspname = current_schema())
loop
insert into dropped_foreign_keys (sql) values (
format('alter table %s add constraint %s %s',t.table_name, t.constraint_name, t.constraint_definition));
execute format('alter table %s drop constraint %s', t.table_name, t.constraint_name);
end loop;
end $$;
重新创建删除的外键约束
do $$ declare t record;
begin
-- order by seq for easier troubleshooting when data does not satisfy FKs
for t in select * from dropped_foreign_keys order by seq loop
execute t.sql;
delete from dropped_foreign_keys where seq = t.seq;
end loop;
end $$;
相關內容
- 已提問 1 年前
- AWS 官方已更新 10 個月前
- AWS 官方已更新 3 年前