diff --git a/src/test/regress/expected/foreign_key_to_reference_table.out b/src/test/regress/expected/foreign_key_to_reference_table.out index 98c85ed14..965ec528d 100644 --- a/src/test/regress/expected/foreign_key_to_reference_table.out +++ b/src/test/regress/expected/foreign_key_to_reference_table.out @@ -1609,6 +1609,39 @@ BEGIN; ROLLBACK; DROP TABLE referenced_table CASCADE; DROP TABLE referencing_table; +-- tests specific to an edgecase in citus 8.x where it was possible to create foreign keys +-- in between colocation groups due to a bug of foreign key to reference tables +CREATE TABLE t1 (a int PRIMARY KEY, b text); +CREATE TABLE t2 (a bigint PRIMARY KEY, b text); +CREATE TABLE r1 (a int PRIMARY KEY, b text); +SELECT create_distributed_table('t1', 'a'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +SELECT create_distributed_table('t2', 'a'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +SELECT create_reference_table('r1'); + create_reference_table +--------------------------------------------------------------------- + +(1 row) + +-- this always fails as it should be +ALTER TABLE t1 ADD CONSTRAINT c1 FOREIGN KEY (a) REFERENCES t2(a); +ERROR: cannot create foreign key constraint since relations are not colocated or not referencing a reference table +DETAIL: A distributed table can only have foreign keys if it is referencing another colocated hash distributed table or a reference table +-- after we create a foreign key to the reference table, that has a lower order by name, +-- we would have been able to create a FK to non-colocated tables +ALTER TABLE t1 ADD CONSTRAINT c2 FOREIGN KEY (a) REFERENCES r1(a); +ALTER TABLE t1 ADD CONSTRAINT c3 FOREIGN KEY (a) REFERENCES t2(a); +ERROR: cannot create foreign key constraint since relations are not colocated or not referencing a reference table +DETAIL: A distributed table can only have foreign keys if it is referencing another colocated hash distributed table or a reference table DROP SCHEMA fkey_reference_table CASCADE; SET search_path TO DEFAULT; RESET client_min_messages; diff --git a/src/test/regress/sql/foreign_key_to_reference_table.sql b/src/test/regress/sql/foreign_key_to_reference_table.sql index d4bfed9b7..c03cca61d 100644 --- a/src/test/regress/sql/foreign_key_to_reference_table.sql +++ b/src/test/regress/sql/foreign_key_to_reference_table.sql @@ -918,6 +918,25 @@ ROLLBACK; DROP TABLE referenced_table CASCADE; DROP TABLE referencing_table; +-- tests specific to an edgecase in citus 8.x where it was possible to create foreign keys +-- in between colocation groups due to a bug of foreign key to reference tables +CREATE TABLE t1 (a int PRIMARY KEY, b text); +CREATE TABLE t2 (a bigint PRIMARY KEY, b text); +CREATE TABLE r1 (a int PRIMARY KEY, b text); + +SELECT create_distributed_table('t1', 'a'); +SELECT create_distributed_table('t2', 'a'); +SELECT create_reference_table('r1'); + +-- this always fails as it should be +ALTER TABLE t1 ADD CONSTRAINT c1 FOREIGN KEY (a) REFERENCES t2(a); + +-- after we create a foreign key to the reference table, that has a lower order by name, +-- we would have been able to create a FK to non-colocated tables +ALTER TABLE t1 ADD CONSTRAINT c2 FOREIGN KEY (a) REFERENCES r1(a); +ALTER TABLE t1 ADD CONSTRAINT c3 FOREIGN KEY (a) REFERENCES t2(a); + + DROP SCHEMA fkey_reference_table CASCADE; SET search_path TO DEFAULT; RESET client_min_messages;