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 implements tests for foreign keys.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
20 ifcapable {!foreignkey} {
25 # Create a table and some data to work with.
30 a INTEGER PRIMARY KEY,
32 REFERENCES t1 ON DELETE CASCADE
35 FOREIGN KEY (b,c) REFERENCES t2(x,y) ON UPDATE CASCADE
42 x INTEGER PRIMARY KEY,
50 a INTEGER REFERENCES t2,
51 b INTEGER REFERENCES t1,
52 FOREIGN KEY (a,b) REFERENCES t2(x,y)
59 CREATE TABLE t4(a integer primary key);
60 CREATE TABLE t5(x references t4);
61 CREATE TABLE t6(x references t4);
62 CREATE TABLE t7(x references t4);
63 CREATE TABLE t8(x references t4);
64 CREATE TABLE t9(x references t4);
65 CREATE TABLE t10(x references t4);
77 CREATE TABLE t5(a PRIMARY KEY, b, c);
82 PRAGMA foreign_key_list(t6);
85 {0 0 t5 e c {NO ACTION} {NO ACTION} NONE} \
86 {1 0 t5 d {} {NO ACTION} {NO ACTION} NONE} \
90 CREATE TABLE t7(d, e, f,
91 FOREIGN KEY (d, e) REFERENCES t5(a, b)
93 PRAGMA foreign_key_list(t7);
96 {0 0 t5 d a {NO ACTION} {NO ACTION} NONE} \
97 {0 1 t5 e b {NO ACTION} {NO ACTION} NONE} \
101 CREATE TABLE t8(d, e, f,
102 FOREIGN KEY (d, e) REFERENCES t5 ON DELETE CASCADE ON UPDATE SET NULL
104 PRAGMA foreign_key_list(t8);
107 {0 0 t5 d {} {SET NULL} CASCADE NONE} \
108 {0 1 t5 e {} {SET NULL} CASCADE NONE} \
112 CREATE TABLE t9(d, e, f,
113 FOREIGN KEY (d, e) REFERENCES t5 ON DELETE CASCADE ON UPDATE SET DEFAULT
115 PRAGMA foreign_key_list(t9);
118 {0 0 t5 d {} {SET DEFAULT} CASCADE NONE} \
119 {0 1 t5 e {} {SET DEFAULT} CASCADE NONE} \
122 sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
125 # Stress the dequoting logic. The first test is not so bad.
126 do_execsql_test fkey1-4.0 {
127 PRAGMA foreign_keys=ON;
128 CREATE TABLE "xx1"("xx2" TEXT PRIMARY KEY, "xx3" TEXT);
129 INSERT INTO "xx1"("xx2","xx3") VALUES('abc','def');
130 CREATE TABLE "xx4"("xx5" TEXT REFERENCES "xx1" ON DELETE CASCADE);
131 INSERT INTO "xx4"("xx5") VALUES('abc');
132 INSERT INTO "xx1"("xx2","xx3") VALUES('uvw','xyz');
133 SELECT 1, "xx5" FROM "xx4";
135 SELECT 2, "xx5" FROM "xx4";
138 # This case is identical to the previous except the "xx" in each name
139 # is changed to a single escaped double-quote character.
140 do_execsql_test fkey1-4.1 {
141 PRAGMA foreign_keys=ON;
142 CREATE TABLE """1"("""2" TEXT PRIMARY KEY, """3" TEXT);
143 INSERT INTO """1"("""2","""3") VALUES('abc','def');
144 CREATE TABLE """4"("""5" TEXT REFERENCES """1" ON DELETE CASCADE);
145 INSERT INTO """4"("""5") VALUES('abc');
146 INSERT INTO """1"("""2","""3") VALUES('uvw','xyz');
147 SELECT 1, """5" FROM """4";
149 SELECT 2, """5" FROM """4";
151 do_execsql_test fkey1-4.2 {
152 PRAGMA table_info="""1";
153 } {0 {"2} TEXT 0 {} 1 1 {"3} TEXT 0 {} 0}
155 #-------------------------------------------------------------------------
157 do_execsql_test fkey1-5.1 {
159 x INTEGER PRIMARY KEY,
160 parent REFERENCES t11 ON DELETE CASCADE
162 INSERT INTO t11 VALUES (1, NULL), (2, 1), (3, 2);
165 # The REPLACE part of this statement deletes the row (2, 1). Then the
166 # DELETE CASCADE caused by deleting that row removes the (3, 2) row. Which
167 # would have been the parent of the new row being inserted. Causing an
170 do_catchsql_test fkey1-5.2 {
171 INSERT OR REPLACE INTO t11 VALUES (2, 3);
172 } {1 {FOREIGN KEY constraint failed}}
174 # Make sure sqlite3_trace() output works with triggers used to implement
178 proc sqltrace {txt} {
180 lappend traceoutput $txt
182 do_test fkey1-5.2.1 {
183 unset -nocomplain traceoutput
185 catch {db eval {INSERT OR REPLACE INTO t11 VALUES(2,3);}}
187 } {{INSERT OR REPLACE INTO t11 VALUES(2,3);} {INSERT OR REPLACE INTO t11 VALUES(2,3);} {INSERT OR REPLACE INTO t11 VALUES(2,3);}}
190 # A similar test to the above.
191 do_execsql_test fkey1-5.3 {
193 Id INTEGER PRIMARY KEY,
194 ParentId INTEGER REFERENCES Foo(Id) ON DELETE CASCADE, C1
196 INSERT OR REPLACE INTO Foo(Id, ParentId, C1) VALUES (1, null, 'A');
197 INSERT OR REPLACE INTO Foo(Id, ParentId, C1) VALUES (2, 1, 'A-2-1');
198 INSERT OR REPLACE INTO Foo(Id, ParentId, C1) VALUES (3, 2, 'A-3-2');
199 INSERT OR REPLACE INTO Foo(Id, ParentId, C1) VALUES (4, 3, 'A-4-3');
201 do_catchsql_test fkey1-5.4 {
202 INSERT OR REPLACE INTO Foo(Id, ParentId, C1) VALUES (2, 3, 'A-2-3');
203 } {1 {FOREIGN KEY constraint failed}}
205 #-------------------------------------------------------------------------
206 # Check that foreign key processing is not fooled by partial indexes
207 # on the parent table.
209 do_execsql_test 6.0 {
210 CREATE TABLE p1(x, y);
211 CREATE UNIQUE INDEX p1x ON p1(x) WHERE y<2;
212 INSERT INTO p1 VALUES(1, 1);
213 CREATE TABLE c1(a REFERENCES p1(x));
216 do_catchsql_test 6.1 {
217 INSERT INTO c1 VALUES(1);
218 } {1 {foreign key mismatch - "c1" referencing "p1"}}
220 do_execsql_test 6.2 {
221 CREATE UNIQUE INDEX p1x2 ON p1(x);
222 INSERT INTO c1 VALUES(1);