3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file tests the PRAGMA defer_foreign_keys and
14 # SQLITE_DBSTATUS_DEFERRED_FKS
16 # EVIDENCE-OF: R-18981-16292 When the defer_foreign_keys PRAGMA is on,
17 # enforcement of all foreign key constraints is delayed until the
18 # outermost transaction is committed.
20 # EVIDENCE-OF: R-28911-57501 The defer_foreign_keys pragma defaults to
21 # OFF so that foreign key constraints are only deferred if they are
22 # created as "DEFERRABLE INITIALLY DEFERRED".
24 set testdir [file dirname $argv0]
25 source $testdir/tester.tcl
27 ifcapable {!foreignkey} {
32 do_execsql_test fkey6-1.0 {
33 PRAGMA defer_foreign_keys;
36 do_execsql_test fkey6-1.1 {
37 PRAGMA foreign_keys=ON;
38 CREATE TABLE t1(x INTEGER PRIMARY KEY);
39 CREATE TABLE t2(y INTEGER PRIMARY KEY,
40 z INTEGER REFERENCES t1(x) DEFERRABLE INITIALLY DEFERRED);
41 CREATE INDEX t2z ON t2(z);
42 CREATE TABLE t3(u INTEGER PRIMARY KEY, v INTEGER REFERENCES t1(x));
43 CREATE INDEX t3v ON t3(v);
44 INSERT INTO t1 VALUES(1),(2),(3),(4),(5);
45 INSERT INTO t2 VALUES(1,1),(2,2);
46 INSERT INTO t3 VALUES(3,3),(4,4);
49 catchsql {DELETE FROM t1 WHERE x=2;}
50 } {1 {FOREIGN KEY constraint failed}}
52 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
57 DELETE FROM t1 WHERE x=1;
61 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 1
64 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
72 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
76 PRAGMA defer_foreign_keys=ON;
78 DELETE FROM t1 WHERE x=3;
82 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
85 # EVIDENCE-OF: R-21752-26913 The defer_foreign_keys pragma is
86 # automatically switched off at each COMMIT or ROLLBACK. Hence, the
87 # defer_foreign_keys pragma must be separately enabled for each
89 do_execsql_test fkey6-1.10.1 {
90 PRAGMA defer_foreign_keys;
92 PRAGMA defer_foreign_keys;
94 PRAGMA defer_foreign_keys=ON;
95 PRAGMA defer_foreign_keys;
97 PRAGMA defer_foreign_keys;
100 do_test fkey6-1.10.2 {
101 catchsql {DELETE FROM t1 WHERE x=3}
102 } {1 {FOREIGN KEY constraint failed}}
108 DELETE FROM t1 WHERE x=1;
110 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
114 DELETE FROM t2 WHERE y=1;
116 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
124 do_execsql_test fkey6-2.1 {
125 CREATE TABLE p1(a PRIMARY KEY);
126 INSERT INTO p1 VALUES('one'), ('two');
127 CREATE TABLE c1(x REFERENCES p1);
128 INSERT INTO c1 VALUES('two'), ('one');
131 do_execsql_test fkey6-2.2 {
133 PRAGMA defer_foreign_keys = 1;
136 PRAGMA defer_foreign_keys;
139 do_execsql_test fkey6-2.3 {
141 PRAGMA defer_foreign_keys = 1;
143 PRAGMA vdbe_trace = 0;
145 PRAGMA defer_foreign_keys;
148 do_execsql_test fkey6-2.4 {
150 PRAGMA defer_foreign_keys = 1;
154 PRAGMA defer_foreign_keys;
157 do_execsql_test fkey6-2.5 {
159 CREATE TABLE p1(a PRIMARY KEY);
160 INSERT INTO p1 VALUES('one'), ('two');
161 CREATE TABLE c1(x REFERENCES p1);
162 INSERT INTO c1 VALUES('two'), ('one');
165 do_execsql_test fkey6-2.6 {
167 PRAGMA defer_foreign_keys = 1;
168 INSERT INTO c1 VALUES('three');
171 PRAGMA defer_foreign_keys;