Index: src/fkey.c ================================================================== --- src/fkey.c +++ src/fkey.c @@ -1161,10 +1161,13 @@ Trigger *pTrigger; /* Trigger definition to return */ int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */ action = pFKey->aAction[iAction]; pTrigger = pFKey->apTrigger[iAction]; + if( (db->flags & SQLITE_DeferFKs) && action==OE_Restrict ){ + return 0; + } if( action!=OE_None && !pTrigger ){ u8 enableLookaside; /* Copy of db->lookaside.bEnabled */ char const *zFrom; /* Name of child table */ int nFrom; /* Length in bytes of zFrom */ Index: test/fkey6.test ================================================================== --- test/fkey6.test +++ test/fkey6.test @@ -21,10 +21,11 @@ # OFF so that foreign key constraints are only deferred if they are # created as "DEFERRABLE INITIALLY DEFERRED". set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix fkey6 ifcapable {!foreignkey} { finish_test return } @@ -168,8 +169,61 @@ INSERT INTO c1 VALUES('three'); DROP TABLE c1; COMMIT; PRAGMA defer_foreign_keys; } {0} + +#-------------------------------------------------------------------------- +# Test that defer_foreign_keys disables RESTRICT. +# +do_execsql_test 3.1 { + CREATE TABLE p2(a PRIMARY KEY, b); + CREATE TABLE c2(x, y REFERENCES p2 ON DELETE RESTRICT ON UPDATE RESTRICT); + INSERT INTO p2 VALUES(1, 'one'); + INSERT INTO p2 VALUES(2, 'two'); + INSERT INTO c2 VALUES('i', 1); +} + +do_catchsql_test 3.2.1 { + BEGIN; + UPDATE p2 SET a=a-1; +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test 3.2.2 { COMMIT } + +do_execsql_test 3.2.3 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + UPDATE p2 SET a=a-1; + COMMIT; +} + +do_execsql_test 3.2.4 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + UPDATE p2 SET a=a-1; +} +do_catchsql_test 3.2.5 { + COMMIT; +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test 3.2.6 { ROLLBACK } + +do_execsql_test 3.3.1 { + CREATE TRIGGER p2t AFTER DELETE ON p2 BEGIN + INSERT INTO p2 VALUES(old.a, 'deleted!'); + END; +} +do_catchsql_test 3.3.2 { + BEGIN; + DELETE FROM p2 WHERE a=1; +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test 3.3.3 { COMMIT } + +do_execsql_test 3.3.4 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + DELETE FROM p2 WHERE a=1; + COMMIT; + SELECT * FROM p2; +} {0 one 1 deleted!} finish_test