summaryrefslogtreecommitdiffstats
path: root/test/fkey8.test
blob: 2d72f6fcf0c84b4225b4535eb114b7b3411004ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 2001 September 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests for foreign keys.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix fkey8

ifcapable {!foreignkey} {
  finish_test
  return
}
do_execsql_test 1.0 { PRAGMA foreign_keys = 1; }


foreach {tn use_stmt sql schema} {
  1   1 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1);
  }

  2.1     0 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE CASCADE);
  }
  2.2   0 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE SET NULL);
  }
  2.3   1 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE SET DEFAULT);
  }

  3   1 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE CASCADE);
    CREATE TRIGGER ct1 AFTER DELETE ON c1 BEGIN
      INSERT INTO p1 VALUES('x');
    END;
  }

  4   1 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE CASCADE, c PRIMARY KEY);
    CREATE TABLE cc1(d REFERENCES c1);
  }

  5.1   0 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE CASCADE, c PRIMARY KEY);
    CREATE TABLE cc1(d REFERENCES c1 ON DELETE CASCADE);
  }
  5.2   0 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE CASCADE, c PRIMARY KEY);
    CREATE TABLE cc1(d REFERENCES c1 ON DELETE SET NULL);
  }
  5.3   1 "DELETE FROM p1" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON DELETE CASCADE, c PRIMARY KEY);
    CREATE TABLE cc1(d REFERENCES c1 ON DELETE SET DEFAULT);
  }

  6.1   1 "UPDATE p1 SET a = ?" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON UPDATE SET NULL, c);
  }
  6.2   0 "UPDATE OR IGNORE p1 SET a = ?" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON UPDATE SET NULL, c);
  }
  6.3   1 "UPDATE OR IGNORE p1 SET a = ?" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b REFERENCES p1 ON UPDATE CASCADE, c);
  }
  6.4   1 "UPDATE OR IGNORE p1 SET a = ?" {
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(b NOT NULL REFERENCES p1 ON UPDATE SET NULL, c);
  }

} {
  drop_all_tables
  do_test 1.$tn {
    execsql $schema
    set stmt [sqlite3_prepare_v2 db $sql -1 dummy]
    set ret [uses_stmt_journal $stmt]
    sqlite3_finalize $stmt
    set ret
  } $use_stmt
}

#-------------------------------------------------------------------------
# The following tests check that foreign key constaint counters are
# correctly updated for any implicit DELETE operations that occur
# when a REPLACE command is executed against a WITHOUT ROWID table
# that has no triggers or auxiliary indexes.
#
reset_db
do_execsql_test 2.1.0 {
  PRAGMA foreign_keys = on;
  CREATE TABLE p1(a PRIMARY KEY, b) WITHOUT ROWID;
  CREATE TABLE c1(x REFERENCES p1 DEFERRABLE INITIALLY DEFERRED);

  INSERT INTO p1 VALUES(1, 'one');
  INSERT INTO p1 VALUES(2, 'two');
  INSERT INTO c1 VALUES(1);
  INSERT INTO c1 VALUES(2);
}

do_catchsql_test 2.1.2 {
  BEGIN;
    DELETE FROM p1 WHERE a=1;
    INSERT OR REPLACE INTO p1 VALUES(2, 'two');
  COMMIT;
} {1 {FOREIGN KEY constraint failed}}

reset_db
do_execsql_test 2.2.0 {
  PRAGMA foreign_keys = on;
  CREATE TABLE p2(a PRIMARY KEY, b);
  CREATE TABLE c2(
    x PRIMARY KEY,
    y REFERENCES p2 DEFERRABLE INITIALLY DEFERRED
  ) WITHOUT ROWID;
}

do_catchsql_test 2.2.1 {
  BEGIN;
    INSERT INTO c2 VALUES(13, 13);
    INSERT OR REPLACE INTO c2 VALUES(13, 13);
    DELETE FROM c2;
  COMMIT;
} {0 {}}

reset_db
do_execsql_test 2.3.0 {
  PRAGMA foreign_keys = on;
  CREATE TABLE p3(a PRIMARY KEY, b) WITHOUT ROWID;
  CREATE TABLE c3(x REFERENCES p3);

  INSERT INTO p3 VALUES(1, 'one');
  INSERT INTO p3 VALUES(2, 'two');
  INSERT INTO c3 VALUES(1);
  INSERT INTO c3 VALUES(2);

  CREATE TRIGGER p3d AFTER DELETE ON p3 WHEN old.a=1 BEGIN
    INSERT OR REPLACE INTO p3 VALUES(2, 'three');
  END;
}

do_catchsql_test 2.3.1 {
  DELETE FROM p3 WHERE a=1
} {1 {FOREIGN KEY constraint failed}}


do_execsql_test 3.0 {
  PRAGMA foreign_keys=ON;
  CREATE TABLE t2(
    a PRIMARY KEY, b, c, d, e,
      FOREIGN KEY(b, c) REFERENCES t2(d, e)
  ) WITHOUT ROWID;
  CREATE UNIQUE INDEX idx ON t2(d, e);

  INSERT INTO t2 VALUES(1, 'one', 'one', 'one', 'one'); -- row is parent of self
  INSERT INTO t2 VALUES(2, 'one', 'one', 'one', NULL);  -- parent is row 1
}

do_catchsql_test 3.1 {
  DELETE FROM t2 WHERE a=1;
} {1 {FOREIGN KEY constraint failed}}

do_execsql_test 4.0 {
  CREATE TABLE t1 (
      c1 PRIMARY KEY,
      c2 NUMERIC,
      FOREIGN KEY(c1) REFERENCES t1(c2)
      ) WITHOUT ROWID ;
  CREATE INDEX t1c1 ON t1(c1);
  CREATE UNIQUE INDEX t1c1unique ON t1(c2);
}
do_catchsql_test 4.1 {
  INSERT OR REPLACE INTO t1 VALUES(10000, 20000);
} {1 {FOREIGN KEY constraint failed}}
do_execsql_test 4.2 {
  INSERT OR REPLACE INTO t1 VALUES(20000, 20000);
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 5.0 {
  PRAGMA foreign_keys = true;
  CREATE TABLE parent(
    p TEXT PRIMARY KEY
  );
  CREATE TABLE child(
    c INTEGER UNIQUE, 
    FOREIGN KEY(c) REFERENCES parent(p) DEFERRABLE INITIALLY DEFERRED
  );
  BEGIN;
    INSERT INTO child VALUES(123);
    INSERT INTO parent VALUES('123');
  COMMIT;
}
do_execsql_test 5.1 {
  PRAGMA integrity_check;
} {ok}

do_execsql_test 5.2 {
  INSERT INTO parent VALUES(1200);
  BEGIN;
    INSERT INTO child VALUES(456);
    UPDATE parent SET p = '456' WHERE p=1200;
  COMMIT;
}
do_execsql_test 5.3 {
  PRAGMA integrity_check;
} {ok}

#-------------------------------------------------------------------------
reset_db
forcedelete test.db2
do_execsql_test 6.1 {
  PRAGMA foreign_keys = on;
  CREATE TABLE c1(b);
  INSERT INTO c1 VALUES(123);
}

do_execsql_test 6.2 {
  ATTACH 'test.db2' AS aux;
  CREATE TABLE aux.p1(a INTEGER PRIMARY KEY);
  CREATE TABLE aux.c1(b REFERENCES p1(a) ON DELETE RESTRICT);

  INSERT INTO aux.p1 VALUES(123);
}

do_execsql_test 6.3 {
  DELETE FROM aux.p1 WHERE a=123;
}


finish_test