@@ -1781,3 +1781,86 @@ INSERT INTO fk_notpartitioned_pk VALUES (1600, 601), (1600, 1601);
1781
1781
ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2
1782
1782
FOR VALUES IN (1600);
1783
1783
-- leave these tables around intentionally
1784
+ -- Test creating a constraint at the parent that already exists in partitions.
1785
+ -- There should be no duplicated constraints, and attempts to drop the
1786
+ -- constraint in partitions should raise appropriate errors.
1787
+ create schema fkpart0
1788
+ create table pkey (a int primary key)
1789
+ create table fk_part (a int) partition by list (a)
1790
+ create table fk_part_1 partition of fk_part
1791
+ (foreign key (a) references fkpart0.pkey) for values in (1)
1792
+ create table fk_part_23 partition of fk_part
1793
+ (foreign key (a) references fkpart0.pkey) for values in (2, 3)
1794
+ partition by list (a)
1795
+ create table fk_part_23_2 partition of fk_part_23 for values in (2);
1796
+ alter table fkpart0.fk_part add foreign key (a) references fkpart0.pkey;
1797
+ \d fkpart0.fk_part_1 \\ -- should have only one FK
1798
+ Table "fkpart0.fk_part_1"
1799
+ Column | Type | Collation | Nullable | Default
1800
+ --------+---------+-----------+----------+---------
1801
+ a | integer | | |
1802
+ Partition of: fkpart0.fk_part FOR VALUES IN (1)
1803
+ Foreign-key constraints:
1804
+ "fk_part_1_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1805
+
1806
+ alter table fkpart0.fk_part_1 drop constraint fk_part_1_a_fkey;
1807
+ ERROR: cannot drop inherited constraint "fk_part_1_a_fkey" of relation "fk_part_1"
1808
+ \d fkpart0.fk_part_23 \\ -- should have only one FK
1809
+ Table "fkpart0.fk_part_23"
1810
+ Column | Type | Collation | Nullable | Default
1811
+ --------+---------+-----------+----------+---------
1812
+ a | integer | | |
1813
+ Partition of: fkpart0.fk_part FOR VALUES IN (2, 3)
1814
+ Partition key: LIST (a)
1815
+ Foreign-key constraints:
1816
+ "fk_part_23_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1817
+ Number of partitions: 1 (Use \d+ to list them.)
1818
+
1819
+ \d fkpart0.fk_part_23_2 \\ -- should have only one FK
1820
+ Table "fkpart0.fk_part_23_2"
1821
+ Column | Type | Collation | Nullable | Default
1822
+ --------+---------+-----------+----------+---------
1823
+ a | integer | | |
1824
+ Partition of: fkpart0.fk_part_23 FOR VALUES IN (2)
1825
+ Foreign-key constraints:
1826
+ "fk_part_23_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1827
+
1828
+ alter table fkpart0.fk_part_23 drop constraint fk_part_23_a_fkey;
1829
+ ERROR: cannot drop inherited constraint "fk_part_23_a_fkey" of relation "fk_part_23"
1830
+ alter table fkpart0.fk_part_23_2 drop constraint fk_part_23_a_fkey;
1831
+ ERROR: cannot drop inherited constraint "fk_part_23_a_fkey" of relation "fk_part_23_2"
1832
+ create table fkpart0.fk_part_4 partition of fkpart0.fk_part for values in (4);
1833
+ \d fkpart0.fk_part_4
1834
+ Table "fkpart0.fk_part_4"
1835
+ Column | Type | Collation | Nullable | Default
1836
+ --------+---------+-----------+----------+---------
1837
+ a | integer | | |
1838
+ Partition of: fkpart0.fk_part FOR VALUES IN (4)
1839
+ Foreign-key constraints:
1840
+ "fk_part_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1841
+
1842
+ alter table fkpart0.fk_part_4 drop constraint fk_part_a_fkey;
1843
+ ERROR: cannot drop inherited constraint "fk_part_a_fkey" of relation "fk_part_4"
1844
+ create table fkpart0.fk_part_56 partition of fkpart0.fk_part
1845
+ for values in (5,6) partition by list (a);
1846
+ create table fkpart0.fk_part_56_5 partition of fkpart0.fk_part_56
1847
+ for values in (5);
1848
+ \d fkpart0.fk_part_56
1849
+ Table "fkpart0.fk_part_56"
1850
+ Column | Type | Collation | Nullable | Default
1851
+ --------+---------+-----------+----------+---------
1852
+ a | integer | | |
1853
+ Partition of: fkpart0.fk_part FOR VALUES IN (5, 6)
1854
+ Partition key: LIST (a)
1855
+ Foreign-key constraints:
1856
+ "fk_part_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1857
+ Number of partitions: 1 (Use \d+ to list them.)
1858
+
1859
+ alter table fkpart0.fk_part_56 drop constraint fk_part_a_fkey;
1860
+ ERROR: cannot drop inherited constraint "fk_part_a_fkey" of relation "fk_part_56"
1861
+ alter table fkpart0.fk_part_56_5 drop constraint fk_part_a_fkey;
1862
+ ERROR: cannot drop inherited constraint "fk_part_a_fkey" of relation "fk_part_56_5"
1863
+ \set VERBOSITY terse \\ -- suppress cascade details
1864
+ drop schema fkpart0 cascade;
1865
+ NOTICE: drop cascades to 2 other objects
1866
+ \set VERBOSITY default
0 commit comments