|
|
|
@ -186,17 +186,17 @@ NOTICE: should see this only if -100 fits in smallint |
|
|
|
|
-- |
|
|
|
|
-- test foreign key error trapping |
|
|
|
|
-- |
|
|
|
|
create temp table master(f1 int primary key); |
|
|
|
|
create temp table slave(f1 int references master deferrable); |
|
|
|
|
insert into master values(1); |
|
|
|
|
insert into slave values(1); |
|
|
|
|
insert into slave values(2); -- fails |
|
|
|
|
ERROR: insert or update on table "slave" violates foreign key constraint "slave_f1_fkey" |
|
|
|
|
DETAIL: Key (f1)=(2) is not present in table "master". |
|
|
|
|
create temp table root(f1 int primary key); |
|
|
|
|
create temp table leaf(f1 int references root deferrable); |
|
|
|
|
insert into root values(1); |
|
|
|
|
insert into leaf values(1); |
|
|
|
|
insert into leaf values(2); -- fails |
|
|
|
|
ERROR: insert or update on table "leaf" violates foreign key constraint "leaf_f1_fkey" |
|
|
|
|
DETAIL: Key (f1)=(2) is not present in table "root". |
|
|
|
|
create function trap_foreign_key(int) returns int as $$ |
|
|
|
|
begin |
|
|
|
|
begin -- start a subtransaction |
|
|
|
|
insert into slave values($1); |
|
|
|
|
insert into leaf values($1); |
|
|
|
|
exception |
|
|
|
|
when foreign_key_violation then |
|
|
|
|
raise notice 'caught foreign_key_violation'; |
|
|
|
@ -238,8 +238,8 @@ begin; |
|
|
|
|
|
|
|
|
|
savepoint x; |
|
|
|
|
set constraints all immediate; -- fails |
|
|
|
|
ERROR: insert or update on table "slave" violates foreign key constraint "slave_f1_fkey" |
|
|
|
|
DETAIL: Key (f1)=(2) is not present in table "master". |
|
|
|
|
ERROR: insert or update on table "leaf" violates foreign key constraint "leaf_f1_fkey" |
|
|
|
|
DETAIL: Key (f1)=(2) is not present in table "root". |
|
|
|
|
rollback to x; |
|
|
|
|
select trap_foreign_key_2(); -- detects FK violation |
|
|
|
|
NOTICE: caught foreign_key_violation |
|
|
|
@ -249,7 +249,7 @@ NOTICE: caught foreign_key_violation |
|
|
|
|
(1 row) |
|
|
|
|
|
|
|
|
|
commit; -- still fails |
|
|
|
|
ERROR: insert or update on table "slave" violates foreign key constraint "slave_f1_fkey" |
|
|
|
|
DETAIL: Key (f1)=(2) is not present in table "master". |
|
|
|
|
ERROR: insert or update on table "leaf" violates foreign key constraint "leaf_f1_fkey" |
|
|
|
|
DETAIL: Key (f1)=(2) is not present in table "root". |
|
|
|
|
drop function trap_foreign_key(int); |
|
|
|
|
drop function trap_foreign_key_2(); |
|
|
|
|