mirror of
https://github.com/postgres/postgres.git
synced 2025-12-10 14:22:35 +03:00
This is mostly the same as an earlier patch I
didn't hear anything about, but which would have broken with the function manager changes anyway. Well, this patch checks that a unique constraint of some form (unique or pk) is on the referenced columns of an FK constraint and that the columns in the referencing table exist at creation time. The former is to move closer to SQL compatibility and the latter is in answer to a bug report. I also added a basic check of this functionality to the alter table and foreign key regression tests. Stephan Szabo sszabo@bigpanda.com
This commit is contained in:
@@ -282,6 +282,14 @@ INSERT INTO tmp2 values (4);
|
||||
INSERT INTO tmp3 values (1,10);
|
||||
INSERT INTO tmp3 values (1,20);
|
||||
INSERT INTO tmp3 values (5,50);
|
||||
-- Try (and fail) to add constraint due to invalid source columns
|
||||
ALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;
|
||||
NOTICE: ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for FOREIGN KEY check(s)
|
||||
ERROR: columns referenced in foreign key constraint not found.
|
||||
-- Try (and fail) to add constraint due to invalide destination columns explicitly given
|
||||
ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
|
||||
NOTICE: ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for FOREIGN KEY check(s)
|
||||
ERROR: UNIQUE constraint matching given keys for referenced table "tmp2" not found
|
||||
-- Try (and fail) to add constraint due to invalid data
|
||||
ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
|
||||
NOTICE: ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for FOREIGN KEY check(s)
|
||||
|
||||
@@ -690,3 +690,16 @@ DROP TABLE FKTABLE;
|
||||
NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "pktable"
|
||||
NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "pktable"
|
||||
DROP TABLE PKTABLE;
|
||||
CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);
|
||||
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
|
||||
CREATE TABLE FKTABLE_FAIL1 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest2) REFERENCES PKTABLE);
|
||||
NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
||||
ERROR: columns referenced in foreign key constraint not found.
|
||||
CREATE TABLE FKTABLE_FAIL2 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest1) REFERENCES PKTABLE(ptest2));
|
||||
NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
||||
ERROR: UNIQUE constraint matching given keys for referenced table "pktable" not found
|
||||
DROP TABLE FKTABLE_FAIL1;
|
||||
ERROR: Relation 'fktable_fail1' does not exist
|
||||
DROP TABLE FKTABLE_FAIL2;
|
||||
ERROR: Relation 'fktable_fail2' does not exist
|
||||
DROP TABLE PKTABLE;
|
||||
|
||||
@@ -180,6 +180,12 @@ INSERT INTO tmp3 values (1,10);
|
||||
INSERT INTO tmp3 values (1,20);
|
||||
INSERT INTO tmp3 values (5,50);
|
||||
|
||||
-- Try (and fail) to add constraint due to invalid source columns
|
||||
ALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;
|
||||
|
||||
-- Try (and fail) to add constraint due to invalide destination columns explicitly given
|
||||
ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
|
||||
|
||||
-- Try (and fail) to add constraint due to invalid data
|
||||
ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
|
||||
|
||||
|
||||
@@ -411,4 +411,10 @@ SELECT * from FKTABLE;
|
||||
DROP TABLE FKTABLE;
|
||||
DROP TABLE PKTABLE;
|
||||
|
||||
CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);
|
||||
CREATE TABLE FKTABLE_FAIL1 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest2) REFERENCES PKTABLE);
|
||||
CREATE TABLE FKTABLE_FAIL2 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest1) REFERENCES PKTABLE(ptest2));
|
||||
|
||||
DROP TABLE FKTABLE_FAIL1;
|
||||
DROP TABLE FKTABLE_FAIL2;
|
||||
DROP TABLE PKTABLE;
|
||||
|
||||
Reference in New Issue
Block a user