@ -21,7 +21,7 @@ INSERT INTO FKTABLE VALUES (3, 4);
INSERT INTO FKTABLE VALUES (NULL, 1);
-- Insert a failed row into FK TABLE
INSERT INTO FKTABLE VALUES (100, 2);
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey "
DETAIL: Key (ftest1)=(100) is not present in table "pktable".
-- Check FKTABLE
SELECT * FROM FKTABLE;
@ -282,7 +282,7 @@ INSERT INTO FKTABLE VALUES (3, 4);
INSERT INTO FKTABLE VALUES (NULL, 1);
-- Insert a failed row into FK TABLE
INSERT INTO FKTABLE VALUES (100, 2);
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey "
DETAIL: Key (ftest1)=(100) is not present in table "pktable".
-- Check FKTABLE
SELECT * FROM FKTABLE;
@ -307,7 +307,7 @@ SELECT * FROM PKTABLE;
-- Delete a row from PK TABLE (should fail)
DELETE FROM PKTABLE WHERE ptest1=1;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "fktable"
ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey " on "fktable"
DETAIL: Key (ptest1)=(1) is still referenced from table "fktable".
-- Delete a row from PK TABLE (should succeed)
DELETE FROM PKTABLE WHERE ptest1=5;
@ -323,7 +323,7 @@ SELECT * FROM PKTABLE;
-- Update a row from PK TABLE (should fail)
UPDATE PKTABLE SET ptest1=0 WHERE ptest1=2;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "fktable"
ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey " on "fktable"
DETAIL: Key (ptest1)=(2) is still referenced from table "fktable".
-- Update a row from PK TABLE (should succeed)
UPDATE PKTABLE SET ptest1=0 WHERE ptest1=4;
@ -750,22 +750,22 @@ CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
-- This next should fail, because inet=int does not exist
CREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable);
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
-- This should also fail for the same reason, but here we
-- give the column name
CREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable(ptest1));
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
-- This should succeed (with a warning), even though they are different types
-- because int=varchar does exist
CREATE TABLE FKTABLE (ftest1 varchar REFERENCES pktable);
WARNING: foreign key constraint "$1 " will require costly sequential scans
WARNING: foreign key constraint "fktable_ftest1_fkey " will require costly sequential scans
DETAIL: Key columns "ftest1" and "ptest1" are of different types: character varying and integer.
DROP TABLE FKTABLE;
-- As should this
CREATE TABLE FKTABLE (ftest1 varchar REFERENCES pktable(ptest1));
WARNING: foreign key constraint "$1 " will require costly sequential scans
WARNING: foreign key constraint "fktable_ftest1_fkey " will require costly sequential scans
DETAIL: Key columns "ftest1" and "ptest1" are of different types: character varying and integer.
DROP TABLE FKTABLE;
DROP TABLE PKTABLE;
@ -774,23 +774,23 @@ CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, PRIMARY KEY(ptest1, ptest2));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
-- This should fail, because we just chose really odd types
CREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable);
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
-- Again, so should this...
CREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest1, ptest2));
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
-- This fails because we mixed up the column ordering
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable);
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest2_fkey " cannot be implemented
DETAIL: Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
-- As does this...
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest1, ptest2));
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest2_fkey " cannot be implemented
DETAIL: Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
-- And again..
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest2, ptest1));
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "ptest2" are of incompatible types: integer and inet.
-- This works...
CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest2, ptest1));
@ -814,19 +814,19 @@ DROP TABLE PKTABLE;
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,
ptest4) REFERENCES pktable(ptest2, ptest1));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "pktable_ptest3_fkey " cannot be implemented
DETAIL: Key columns "ptest3" and "ptest2" are of incompatible types: integer and inet.
-- Nor should this... (same reason, we have 4,3 referencing 1,2 which mismatches types
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,
ptest3) REFERENCES pktable(ptest1, ptest2));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "pktable_ptest4_fkey " cannot be implemented
DETAIL: Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.
-- Not this one either... Same as the last one except we didn't defined the columns being referenced.
CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,
ptest3) REFERENCES pktable);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "pktable_ptest4_fkey " cannot be implemented
DETAIL: Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.
--
-- Now some cases with inheritance
@ -841,19 +841,19 @@ insert into pktable(base1) values (1);
insert into pktable(base1) values (2);
-- let's insert a non-existant fktable value
insert into fktable(ftest1) values (3);
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey "
DETAIL: Key (ftest1)=(3) is not present in table "pktable".
-- let's make a valid row for that
insert into pktable(base1) values (3);
insert into fktable(ftest1) values (3);
-- let's try removing a row that should fail from pktable
delete from pktable where base1>2;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "fktable"
ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey " on "fktable"
DETAIL: Key (base1)=(3) is still referenced from table "fktable".
-- okay, let's try updating all of the base1 values to *4
-- which should fail.
update pktable set base1=base1*4;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "fktable"
ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey " on "fktable"
DETAIL: Key (base1)=(3) is still referenced from table "fktable".
-- okay, let's try an update that should work.
update pktable set base1=base1*4 where base1<3;
@ -869,19 +869,19 @@ insert into pktable(base1, ptest1) values (1, 1);
insert into pktable(base1, ptest1) values (2, 2);
-- let's insert a non-existant fktable value
insert into fktable(ftest1, ftest2) values (3, 1);
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey "
DETAIL: Key (ftest1,ftest2)=(3,1) is not present in table "pktable".
-- let's make a valid row for that
insert into pktable(base1,ptest1) values (3, 1);
insert into fktable(ftest1, ftest2) values (3, 1);
-- let's try removing a row that should fail from pktable
delete from pktable where base1>2;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "fktable"
ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey " on "fktable"
DETAIL: Key (base1,ptest1)=(3,1) is still referenced from table "fktable".
-- okay, let's try updating all of the base1 values to *4
-- which should fail.
update pktable set base1=base1*4;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "fktable"
ERROR: update or delete on "pktable" violates foreign key constraint "fktable_ftest1_fkey " on "fktable"
DETAIL: Key (base1,ptest1)=(3,1) is still referenced from table "fktable".
-- okay, let's try an update that should work.
update pktable set base1=base1*4 where base1<3;
@ -902,15 +902,15 @@ insert into pktable (base1, ptest1, base2, ptest2) values (2, 2, 2, 1);
insert into pktable (base1, ptest1, base2, ptest2) values (1, 3, 2, 2);
-- fails (3,2) isn't in base1, ptest1
insert into pktable (base1, ptest1, base2, ptest2) values (2, 3, 3, 2);
ERROR: insert or update on table "pktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "pktable" violates foreign key constraint "pktable_base2_fkey "
DETAIL: Key (base2,ptest2)=(3,2) is not present in table "pktable".
-- fails (2,2) is being referenced
delete from pktable where base1=2;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "pktable"
ERROR: update or delete on "pktable" violates foreign key constraint "pktable_base2_fkey " on "pktable"
DETAIL: Key (base1,ptest1)=(2,2) is still referenced from table "pktable".
-- fails (1,1) is being referenced (twice)
update pktable set base1=3 where base1=1;
ERROR: update or delete on "pktable" violates foreign key constraint "$1 " on "pktable"
ERROR: update or delete on "pktable" violates foreign key constraint "pktable_base2_fkey " on "pktable"
DETAIL: Key (base1,ptest1)=(1,1) is still referenced from table "pktable".
-- this sequence of two deletes will work, since after the first there will be no (2,*) references
delete from pktable where base2=2;
@ -923,20 +923,20 @@ create table pktable(ptest1 inet, primary key(base1, ptest1)) inherits (pktable_
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
-- just generally bad types (with and without column references on the referenced table)
create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable);
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.
create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable(base1, ptest1));
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.
-- let's mix up which columns reference which
create table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable);
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest2_fkey " cannot be implemented
DETAIL: Key columns "ftest2" and "base1" are of incompatible types: inet and integer.
create table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable(base1, ptest1));
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest2_fkey " cannot be implemented
DETAIL: Key columns "ftest2" and "base1" are of incompatible types: inet and integer.
create table fktable(ftest1 int, ftest2 inet, foreign key(ftest1, ftest2) references pktable(ptest1, base1));
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "fktable_ftest1_fkey " cannot be implemented
DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: integer and inet.
drop table pktable;
drop table pktable_base;
@ -945,22 +945,22 @@ create table pktable_base(base1 int not null, base2 int);
create table pktable(ptest1 inet, ptest2 inet[], primary key(base1, ptest1), foreign key(base2, ptest2) references
pktable(base1, ptest1)) inherits (pktable_base);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "pktable_base2_fkey " cannot be implemented
DETAIL: Key columns "ptest2" and "ptest1" are of incompatible types: inet[] and inet.
create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(base2, ptest2) references
pktable(ptest1, base1)) inherits (pktable_base);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "pktable_base2_fkey " cannot be implemented
DETAIL: Key columns "base2" and "ptest1" are of incompatible types: integer and inet.
create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(ptest2, base2) references
pktable(base1, ptest1)) inherits (pktable_base);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "pktable_ptest2_fkey " cannot be implemented
DETAIL: Key columns "ptest2" and "base1" are of incompatible types: inet and integer.
create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(ptest2, base2) references
pktable(base1, ptest1)) inherits (pktable_base);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pktable_pkey" for table "pktable"
ERROR: foreign key constraint "$1 " cannot be implemented
ERROR: foreign key constraint "pktable_ptest2_fkey " cannot be implemented
DETAIL: Key columns "ptest2" and "base1" are of incompatible types: inet and integer.
drop table pktable;
ERROR: table "pktable" does not exist
@ -982,7 +982,7 @@ CREATE TABLE fktable (
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "fktable_pkey" for table "fktable"
-- default to immediate: should fail
INSERT INTO fktable VALUES (5, 10);
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey "
DETAIL: Key (fk)=(10) is not present in table "pktable".
-- explicitely defer the constraint
BEGIN;
@ -1012,7 +1012,7 @@ BEGIN;
SET CONSTRAINTS ALL IMMEDIATE;
-- should fail
INSERT INTO fktable VALUES (500, 1000);
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey "
DETAIL: Key (fk)=(1000) is not present in table "pktable".
COMMIT;
DROP TABLE fktable, pktable;
@ -1036,7 +1036,7 @@ SET CONSTRAINTS ALL DEFERRED;
INSERT INTO fktable VALUES (1000, 2000);
-- should cause transaction abort, due to preceding error
SET CONSTRAINTS ALL IMMEDIATE;
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey "
DETAIL: Key (fk)=(2000) is not present in table "pktable".
INSERT INTO pktable VALUES (2000, 3); -- too late
ERROR: current transaction is aborted, commands ignored until end of transaction block
@ -1058,7 +1058,7 @@ BEGIN;
INSERT INTO fktable VALUES (100, 200);
-- error here on commit
COMMIT;
ERROR: insert or update on table "fktable" violates foreign key constraint "$1 "
ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey "
DETAIL: Key (fk)=(200) is not present in table "pktable".
-- test notice about expensive referential integrity checks,
-- where the index cannot be used because of type incompatibilities.