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 #***********************************************************************
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
17 ifcapable !foreignkey||!trigger {
21 source $testdir/malloc_common.tcl
23 do_malloc_test fkey_malloc-1 -sqlprep {
24 PRAGMA foreign_keys = 1;
25 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
26 CREATE TABLE t2(x REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE);
28 INSERT INTO t1 VALUES('aaa', 1);
29 INSERT INTO t2 VALUES('aaa');
30 UPDATE t1 SET a = 'bbb';
34 do_malloc_test fkey_malloc-2 -sqlprep {
35 PRAGMA foreign_keys = 1;
36 CREATE TABLE t1(a, b, UNIQUE(a, b));
39 FOREIGN KEY(x, y) REFERENCES t1(a, b) DEFERRABLE INITIALLY DEFERRED
42 INSERT INTO t2 VALUES('a', 'b');
43 INSERT INTO t1 VALUES('a', 'b');
44 UPDATE t1 SET a = 'c';
46 INSERT INTO t2 VALUES('d', 'b');
47 UPDATE t2 SET x = 'c';
51 do_malloc_test fkey_malloc-3 -sqlprep {
52 PRAGMA foreign_keys = 1;
53 CREATE TABLE t1(x INTEGER PRIMARY KEY);
54 CREATE TABLE t2(y DEFAULT 14 REFERENCES t1(x) ON UPDATE SET DEFAULT);
55 CREATE TABLE t3(y REFERENCES t1 ON UPDATE SET NULL);
56 INSERT INTO t1 VALUES(13);
57 INSERT INTO t2 VALUES(13);
58 INSERT INTO t3 VALUES(13);
63 proc catch_fk_error {zSql} {
64 set rc [catch {db eval $zSql} msg]
68 if {[string match {*foreign key*} $msg]} {
71 if {$msg eq "out of memory"} {
77 do_malloc_test fkey_malloc-4 -sqlprep {
78 PRAGMA foreign_keys = 1;
79 CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE);
80 CREATE TABLE t2(z REFERENCES t1(x), a REFERENCES t1(y));
82 CREATE TABLE t4(z REFERENCES t3);
83 CREATE TABLE t5(x, y);
84 CREATE TABLE t6(z REFERENCES t5(x));
85 CREATE INDEX i51 ON t5(x);
86 CREATE INDEX i52 ON t5(y, x);
87 INSERT INTO t1 VALUES(1, 2);
89 catch_fk_error {INSERT INTO t2 VALUES(1, 3)}
90 catch_fk_error {INSERT INTO t4 VALUES(2)}
91 catch_fk_error {INSERT INTO t6 VALUES(2)}
94 do_malloc_test fkey_malloc-5 -sqlprep {
95 PRAGMA foreign_keys = 1;
96 CREATE TABLE t1(x, y, PRIMARY KEY(x, y));
97 CREATE TABLE t2(a, b, FOREIGN KEY(a, b) REFERENCES t1 ON UPDATE CASCADE);
98 INSERT INTO t1 VALUES(1, 2);
99 INSERT INTO t2 VALUES(1, 2);
104 do_malloc_test fkey_malloc-6 -sqlprep {
105 PRAGMA foreign_keys = 1;
108 y REFERENCES t1 ON DELETE RESTRICT ON UPDATE SET DEFAULT
110 INSERT INTO t1 VALUES('abc', 'abc');
111 INSERT INTO t1 VALUES('def', 'def');
113 INSERT INTO t1 VALUES('ghi', 'ghi');
114 DELETE FROM t1 WHERE rowid>1;
115 UPDATE t1 SET x='jkl', y='jkl';
118 do_malloc_test fkey_malloc-7 -sqlprep {
119 PRAGMA foreign_keys = 1;
120 CREATE TABLE x(a, b, PRIMARY KEY(a, b));
122 FOREIGN KEY(d, c) REFERENCES x DEFERRABLE INITIALLY DEFERRED
124 CREATE TABLE z(e, f, FOREIGN KEY(e, f) REFERENCES x);