postgresql 更改表集架构后键松动

93ze6v8z  于 2023-06-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(127)

在PostgreSQL 12.5上遇到问题

ALTER TABLE old_shcema.my_table SET new_schema

首先-一切正常,除了触发器之外的所有内容都已传输,但如果您尝试返回

ALTER TABLE new_schema.my_table SET old_schema

然后键,外键将丢失
原因何在?是我做错了什么,还是这是一个postgres bug?

qnakjoqk

qnakjoqk1#

这里不做(PostgreSQL v14)。

richardh=> CREATE SCHEMA a;
CREATE SCHEMA
richardh=> CREATE SCHEMA b;
CREATE SCHEMA
richardh=> CREATE TABLE a.t1 (id int PRIMARY KEY);
CREATE TABLE
richardh=> CREATE TABLE a.t2 (idref int NOT NULL REFERENCES a.t1);
CREATE TABLE
richardh=> ALTER TABLE a.t1 SET SCHEMA b;
ALTER TABLE
richardh=> \d a.t2
                   Table "a.t2"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 idref  | integer |           | not null | 
Foreign-key constraints:
    "t2_idref_fkey" FOREIGN KEY (idref) REFERENCES b.t1(id)

richardh=> ALTER TABLE a.t1 SET SCHEMA a;
ERROR:  relation "a.t1" does not exist
richardh=> ALTER TABLE b.t1 SET SCHEMA a;
ALTER TABLE
richardh=> \d a.t2
                   Table "a.t2"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 idref  | integer |           | not null | 
Foreign-key constraints:
    "t2_idref_fkey" FOREIGN KEY (idref) REFERENCES a.t1(id)

相关问题