forked from sqlite/sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfkey2.test
2047 lines (1948 loc) · 60.8 KB
/
fkey2.test
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 2009 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
ifcapable {!foreignkey||!trigger} {
finish_test
return
}
#-------------------------------------------------------------------------
# Test structure:
#
# fkey2-1.*: Simple tests to check that immediate and deferred foreign key
# constraints work when not inside a transaction.
#
# fkey2-2.*: Tests to verify that deferred foreign keys work inside
# explicit transactions (i.e that processing really is deferred).
#
# fkey2-3.*: Tests that a statement transaction is rolled back if an
# immediate foreign key constraint is violated.
#
# fkey2-4.*: Test that FK actions may recurse even when recursive triggers
# are disabled.
#
# fkey2-5.*: Check that if foreign-keys are enabled, it is not possible
# to write to an FK column using the incremental blob API.
#
# fkey2-6.*: Test that FK processing is automatically disabled when
# running VACUUM.
#
# fkey2-7.*: Test using an IPK as the key in the child (referencing) table.
#
# fkey2-8.*: Test that enabling/disabling foreign key support while a
# transaction is active is not possible.
#
# fkey2-9.*: Test SET DEFAULT actions.
#
# fkey2-10.*: Test errors.
#
# fkey2-11.*: Test CASCADE actions.
#
# fkey2-12.*: Test RESTRICT actions.
#
# fkey2-13.*: Test that FK processing is performed when a row is REPLACED by
# an UPDATE or INSERT statement.
#
# fkey2-14.*: Test the ALTER TABLE and DROP TABLE commands.
#
# fkey2-15.*: Test that if there are no (known) outstanding foreign key
# constraint violations in the database, inserting into a parent
# table or deleting from a child table does not cause SQLite
# to check if this has repaired an outstanding violation.
#
# fkey2-16.*: Test that rows that refer to themselves may be inserted,
# updated and deleted.
#
# fkey2-17.*: Test that the "count_changes" pragma does not interfere with
# FK constraint processing.
#
# fkey2-18.*: Test that the authorization callback is invoked when processing
# FK constraints.
#
# fkey2-20.*: Test that ON CONFLICT clauses specified as part of statements
# do not affect the operation of FK constraints.
#
# fkey2-genfkey.*: Tests that were used with the shell tool .genfkey
# command. Recycled to test the built-in implementation.
#
# fkey2-dd08e5.*: Tests to verify that ticket dd08e5a988d00decc4a543daa8d
# has been fixed.
#
execsql { PRAGMA foreign_keys = on }
set FkeySimpleSchema {
PRAGMA foreign_keys = on;
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE TABLE t2(c REFERENCES t1(a) /D/ , d);
CREATE TABLE t3(a PRIMARY KEY, b);
CREATE TABLE t4(c REFERENCES t3 /D/, d);
CREATE TABLE t7(a, b INTEGER PRIMARY KEY);
CREATE TABLE t8(c REFERENCES t7 /D/, d);
CREATE TABLE t9(a REFERENCES nosuchtable, b);
CREATE TABLE t10(a REFERENCES t9(c) /D/, b);
}
set FkeySimpleTests {
1.1 "INSERT INTO t2 VALUES(1, 3)" {1 {FOREIGN KEY constraint failed}}
1.2 "INSERT INTO t1 VALUES(1, 2)" {0 {}}
1.3 "INSERT INTO t2 VALUES(1, 3)" {0 {}}
1.4 "INSERT INTO t2 VALUES(2, 4)" {1 {FOREIGN KEY constraint failed}}
1.5 "INSERT INTO t2 VALUES(NULL, 4)" {0 {}}
1.6 "UPDATE t2 SET c=2 WHERE d=4" {1 {FOREIGN KEY constraint failed}}
1.7 "UPDATE t2 SET c=1 WHERE d=4" {0 {}}
1.9 "UPDATE t2 SET c=1 WHERE d=4" {0 {}}
1.10 "UPDATE t2 SET c=NULL WHERE d=4" {0 {}}
1.11 "DELETE FROM t1 WHERE a=1" {1 {FOREIGN KEY constraint failed}}
1.12 "UPDATE t1 SET a = 2" {1 {FOREIGN KEY constraint failed}}
1.13 "UPDATE t1 SET a = 1" {0 {}}
2.1 "INSERT INTO t4 VALUES(1, 3)" {1 {FOREIGN KEY constraint failed}}
2.2 "INSERT INTO t3 VALUES(1, 2)" {0 {}}
2.3 "INSERT INTO t4 VALUES(1, 3)" {0 {}}
4.1 "INSERT INTO t8 VALUES(1, 3)" {1 {FOREIGN KEY constraint failed}}
4.2 "INSERT INTO t7 VALUES(2, 1)" {0 {}}
4.3 "INSERT INTO t8 VALUES(1, 3)" {0 {}}
4.4 "INSERT INTO t8 VALUES(2, 4)" {1 {FOREIGN KEY constraint failed}}
4.5 "INSERT INTO t8 VALUES(NULL, 4)" {0 {}}
4.6 "UPDATE t8 SET c=2 WHERE d=4" {1 {FOREIGN KEY constraint failed}}
4.7 "UPDATE t8 SET c=1 WHERE d=4" {0 {}}
4.9 "UPDATE t8 SET c=1 WHERE d=4" {0 {}}
4.10 "UPDATE t8 SET c=NULL WHERE d=4" {0 {}}
4.11 "DELETE FROM t7 WHERE b=1" {1 {FOREIGN KEY constraint failed}}
4.12 "UPDATE t7 SET b = 2" {1 {FOREIGN KEY constraint failed}}
4.13 "UPDATE t7 SET b = 1" {0 {}}
4.14 "INSERT INTO t8 VALUES('a', 'b')" {1 {FOREIGN KEY constraint failed}}
4.15 "UPDATE t7 SET b = 5" {1 {FOREIGN KEY constraint failed}}
4.16 "UPDATE t7 SET rowid = 5" {1 {FOREIGN KEY constraint failed}}
4.17 "UPDATE t7 SET a = 10" {0 {}}
5.1 "INSERT INTO t9 VALUES(1, 3)" {1 {no such table: main.nosuchtable}}
5.2 "INSERT INTO t10 VALUES(1, 3)"
{1 {foreign key mismatch - "t10" referencing "t9"}}
}
do_test fkey2-1.1.0 {
execsql [string map {/D/ {}} $FkeySimpleSchema]
} {}
foreach {tn zSql res} $FkeySimpleTests {
do_test fkey2-1.1.$tn.1 { catchsql $zSql } $res
do_test fkey2-1.1.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {}
do_test fkey2-1.1.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {}
do_test fkey2-1.1.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {}
do_test fkey2-1.1.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {}
do_test fkey2-1.1.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {}
do_test fkey2-1.1.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {}
}
drop_all_tables
do_test fkey2-1.2.0 {
execsql [string map {/D/ {DEFERRABLE INITIALLY DEFERRED}} $FkeySimpleSchema]
} {}
foreach {tn zSql res} $FkeySimpleTests {
do_test fkey2-1.2.$tn { catchsql $zSql } $res
do_test fkey2-1.2.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {}
do_test fkey2-1.2.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {}
do_test fkey2-1.2.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {}
do_test fkey2-1.2.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {}
do_test fkey2-1.2.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {}
do_test fkey2-1.2.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {}
}
drop_all_tables
do_test fkey2-1.3.0 {
execsql [string map {/D/ {}} $FkeySimpleSchema]
execsql { PRAGMA count_changes = 1 }
} {}
foreach {tn zSql res} $FkeySimpleTests {
if {$res == "0 {}"} { set res {0 1} }
do_test fkey2-1.3.$tn { catchsql $zSql } $res
do_test fkey2-1.3.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {}
do_test fkey2-1.3.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {}
do_test fkey2-1.3.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {}
do_test fkey2-1.3.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {}
do_test fkey2-1.3.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {}
do_test fkey2-1.3.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {}
}
execsql { PRAGMA count_changes = 0 }
drop_all_tables
do_test fkey2-1.4.0 {
execsql [string map {/D/ {}} $FkeySimpleSchema]
execsql { PRAGMA count_changes = 1 }
} {}
foreach {tn zSql res} $FkeySimpleTests {
if {$res == "0 {}"} { set res {0 1} }
execsql BEGIN
do_test fkey2-1.4.$tn { catchsql $zSql } $res
execsql COMMIT
}
execsql { PRAGMA count_changes = 0 }
drop_all_tables
# Special test: When the parent key is an IPK, make sure the affinity of
# the IPK is not applied to the child key value before it is inserted
# into the child table.
do_test fkey2-1.5.1 {
execsql {
CREATE TABLE i(i INTEGER PRIMARY KEY);
CREATE TABLE j(j REFERENCES i);
INSERT INTO i VALUES(35);
INSERT INTO j VALUES('35.0');
SELECT j, typeof(j) FROM j;
}
} {35.0 text}
do_test fkey2-1.5.2 {
catchsql { DELETE FROM i }
} {1 {FOREIGN KEY constraint failed}}
# Same test using a regular primary key with integer affinity.
drop_all_tables
do_test fkey2-1.6.1 {
execsql {
CREATE TABLE i(i INT UNIQUE);
CREATE TABLE j(j REFERENCES i(i));
INSERT INTO i VALUES('35.0');
INSERT INTO j VALUES('35.0');
SELECT j, typeof(j) FROM j;
SELECT i, typeof(i) FROM i;
}
} {35.0 text 35 integer}
do_test fkey2-1.6.2 {
catchsql { DELETE FROM i }
} {1 {FOREIGN KEY constraint failed}}
# Use a collation sequence on the parent key.
drop_all_tables
do_test fkey2-1.7.1 {
execsql {
CREATE TABLE i(i TEXT COLLATE nocase PRIMARY KEY);
CREATE TABLE j(j TEXT COLLATE binary REFERENCES i(i));
INSERT INTO i VALUES('SQLite');
INSERT INTO j VALUES('sqlite');
}
catchsql { DELETE FROM i }
} {1 {FOREIGN KEY constraint failed}}
# Use the parent key collation even if it is default and the child key
# has an explicit value.
drop_all_tables
do_test fkey2-1.7.2 {
execsql {
CREATE TABLE i(i TEXT PRIMARY KEY); -- Colseq is "BINARY"
CREATE TABLE j(j TEXT COLLATE nocase REFERENCES i(i));
INSERT INTO i VALUES('SQLite');
}
catchsql { INSERT INTO j VALUES('sqlite') }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-1.7.3 {
execsql {
INSERT INTO i VALUES('sqlite');
INSERT INTO j VALUES('sqlite');
DELETE FROM i WHERE i = 'SQLite';
}
catchsql { DELETE FROM i WHERE i = 'sqlite' }
} {1 {FOREIGN KEY constraint failed}}
#-------------------------------------------------------------------------
# This section (test cases fkey2-2.*) contains tests to check that the
# deferred foreign key constraint logic works.
#
proc fkey2-2-test {tn nocommit sql {res {}}} {
if {$res eq "FKV"} {
set expected {1 {FOREIGN KEY constraint failed}}
} else {
set expected [list 0 $res]
}
do_test fkey2-2.$tn [list catchsql $sql] $expected
if {$nocommit} {
do_test fkey2-2.${tn}c {
catchsql COMMIT
} {1 {FOREIGN KEY constraint failed}}
}
}
fkey2-2-test 1 0 {
CREATE TABLE node(
nodeid PRIMARY KEY,
parent REFERENCES node DEFERRABLE INITIALLY DEFERRED
);
CREATE TABLE leaf(
cellid PRIMARY KEY,
parent REFERENCES node DEFERRABLE INITIALLY DEFERRED
);
}
fkey2-2-test 1 0 "INSERT INTO node VALUES(1, 0)" FKV
fkey2-2-test 2 0 "BEGIN"
fkey2-2-test 3 1 "INSERT INTO node VALUES(1, 0)"
fkey2-2-test 4 0 "UPDATE node SET parent = NULL"
fkey2-2-test 5 0 "COMMIT"
fkey2-2-test 6 0 "SELECT * FROM node" {1 {}}
fkey2-2-test 7 0 "BEGIN"
fkey2-2-test 8 1 "INSERT INTO leaf VALUES('a', 2)"
fkey2-2-test 9 1 "INSERT INTO node VALUES(2, 0)"
fkey2-2-test 10 0 "UPDATE node SET parent = 1 WHERE nodeid = 2"
fkey2-2-test 11 0 "COMMIT"
fkey2-2-test 12 0 "SELECT * FROM node" {1 {} 2 1}
fkey2-2-test 13 0 "SELECT * FROM leaf" {a 2}
fkey2-2-test 14 0 "BEGIN"
fkey2-2-test 15 1 "DELETE FROM node WHERE nodeid = 2"
fkey2-2-test 16 0 "INSERT INTO node VALUES(2, NULL)"
fkey2-2-test 17 0 "COMMIT"
fkey2-2-test 18 0 "SELECT * FROM node" {1 {} 2 {}}
fkey2-2-test 19 0 "SELECT * FROM leaf" {a 2}
fkey2-2-test 20 0 "BEGIN"
fkey2-2-test 21 0 "INSERT INTO leaf VALUES('b', 1)"
fkey2-2-test 22 0 "SAVEPOINT save"
fkey2-2-test 23 0 "DELETE FROM node WHERE nodeid = 1"
fkey2-2-test 24 0 "ROLLBACK TO save"
fkey2-2-test 25 0 "COMMIT"
fkey2-2-test 26 0 "SELECT * FROM node" {1 {} 2 {}}
fkey2-2-test 27 0 "SELECT * FROM leaf" {a 2 b 1}
fkey2-2-test 28 0 "BEGIN"
fkey2-2-test 29 0 "INSERT INTO leaf VALUES('c', 1)"
fkey2-2-test 30 0 "SAVEPOINT save"
fkey2-2-test 31 0 "DELETE FROM node WHERE nodeid = 1"
fkey2-2-test 32 1 "RELEASE save"
fkey2-2-test 33 1 "DELETE FROM leaf WHERE cellid = 'b'"
fkey2-2-test 34 0 "DELETE FROM leaf WHERE cellid = 'c'"
fkey2-2-test 35 0 "COMMIT"
fkey2-2-test 36 0 "SELECT * FROM node" {2 {}}
fkey2-2-test 37 0 "SELECT * FROM leaf" {a 2}
fkey2-2-test 38 0 "SAVEPOINT outer"
fkey2-2-test 39 1 "INSERT INTO leaf VALUES('d', 3)"
fkey2-2-test 40 1 "RELEASE outer" FKV
fkey2-2-test 41 1 "INSERT INTO leaf VALUES('e', 3)"
fkey2-2-test 42 0 "INSERT INTO node VALUES(3, 2)"
fkey2-2-test 43 0 "RELEASE outer"
fkey2-2-test 44 0 "SAVEPOINT outer"
fkey2-2-test 45 1 "DELETE FROM node WHERE nodeid=3"
fkey2-2-test 47 0 "INSERT INTO node VALUES(3, 2)"
fkey2-2-test 48 0 "ROLLBACK TO outer"
fkey2-2-test 49 0 "RELEASE outer"
fkey2-2-test 50 0 "SAVEPOINT outer"
fkey2-2-test 51 1 "INSERT INTO leaf VALUES('f', 4)"
fkey2-2-test 52 1 "SAVEPOINT inner"
fkey2-2-test 53 1 "INSERT INTO leaf VALUES('g', 4)"
fkey2-2-test 54 1 "RELEASE outer" FKV
fkey2-2-test 55 1 "ROLLBACK TO inner"
fkey2-2-test 56 0 "COMMIT" FKV
fkey2-2-test 57 0 "INSERT INTO node VALUES(4, NULL)"
fkey2-2-test 58 0 "RELEASE outer"
fkey2-2-test 59 0 "SELECT * FROM node" {2 {} 3 2 4 {}}
fkey2-2-test 60 0 "SELECT * FROM leaf" {a 2 d 3 e 3 f 4}
# The following set of tests check that if a statement that affects
# multiple rows violates some foreign key constraints, then strikes a
# constraint that causes the statement-transaction to be rolled back,
# the deferred constraint counter is correctly reset to the value it
# had before the statement-transaction was opened.
#
fkey2-2-test 61 0 "BEGIN"
fkey2-2-test 62 0 "DELETE FROM leaf"
fkey2-2-test 63 0 "DELETE FROM node"
fkey2-2-test 64 1 "INSERT INTO leaf VALUES('a', 1)"
fkey2-2-test 65 1 "INSERT INTO leaf VALUES('b', 2)"
fkey2-2-test 66 1 "INSERT INTO leaf VALUES('c', 1)"
do_test fkey2-2-test-67 {
catchsql "INSERT INTO node SELECT parent, 3 FROM leaf"
} {1 {UNIQUE constraint failed: node.nodeid}}
fkey2-2-test 68 0 "COMMIT" FKV
fkey2-2-test 69 1 "INSERT INTO node VALUES(1, NULL)"
fkey2-2-test 70 0 "INSERT INTO node VALUES(2, NULL)"
fkey2-2-test 71 0 "COMMIT"
fkey2-2-test 72 0 "BEGIN"
fkey2-2-test 73 1 "DELETE FROM node"
fkey2-2-test 74 0 "INSERT INTO node(nodeid) SELECT DISTINCT parent FROM leaf"
fkey2-2-test 75 0 "COMMIT"
#-------------------------------------------------------------------------
# Test cases fkey2-3.* test that a program that executes foreign key
# actions (CASCADE, SET DEFAULT, SET NULL etc.) or tests FK constraints
# opens a statement transaction if required.
#
# fkey2-3.1.*: Test UPDATE statements.
# fkey2-3.2.*: Test DELETE statements.
#
drop_all_tables
do_test fkey2-3.1.1 {
execsql {
CREATE TABLE ab(a PRIMARY KEY, b);
CREATE TABLE cd(
c PRIMARY KEY REFERENCES ab ON UPDATE CASCADE ON DELETE CASCADE,
d
);
CREATE TABLE ef(
e REFERENCES cd ON UPDATE CASCADE,
f, CHECK (e!=5)
);
}
} {}
do_test fkey2-3.1.2 {
execsql {
INSERT INTO ab VALUES(1, 'b');
INSERT INTO cd VALUES(1, 'd');
INSERT INTO ef VALUES(1, 'e');
}
} {}
do_test fkey2-3.1.3 {
catchsql { UPDATE ab SET a = 5 }
} {1 {CHECK constraint failed: e!=5}}
do_test fkey2-3.1.4 {
execsql { SELECT * FROM ab }
} {1 b}
do_test fkey2-3.1.4 {
execsql BEGIN;
catchsql { UPDATE ab SET a = 5 }
} {1 {CHECK constraint failed: e!=5}}
do_test fkey2-3.1.5 {
execsql COMMIT;
execsql { SELECT * FROM ab; SELECT * FROM cd; SELECT * FROM ef }
} {1 b 1 d 1 e}
do_test fkey2-3.2.1 {
execsql BEGIN;
catchsql { DELETE FROM ab }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-3.2.2 {
execsql COMMIT
execsql { SELECT * FROM ab; SELECT * FROM cd; SELECT * FROM ef }
} {1 b 1 d 1 e}
#-------------------------------------------------------------------------
# Test cases fkey2-4.* test that recursive foreign key actions
# (i.e. CASCADE) are allowed even if recursive triggers are disabled.
#
drop_all_tables
do_test fkey2-4.1 {
execsql {
CREATE TABLE t1(
node PRIMARY KEY,
parent REFERENCES t1 ON DELETE CASCADE
);
CREATE TABLE t2(node PRIMARY KEY, parent);
CREATE TRIGGER t2t AFTER DELETE ON t2 BEGIN
DELETE FROM t2 WHERE parent = old.node;
END;
INSERT INTO t1 VALUES(1, NULL);
INSERT INTO t1 VALUES(2, 1);
INSERT INTO t1 VALUES(3, 1);
INSERT INTO t1 VALUES(4, 2);
INSERT INTO t1 VALUES(5, 2);
INSERT INTO t1 VALUES(6, 3);
INSERT INTO t1 VALUES(7, 3);
INSERT INTO t2 SELECT * FROM t1;
}
} {}
do_test fkey2-4.2 {
execsql { PRAGMA recursive_triggers = off }
execsql {
BEGIN;
DELETE FROM t1 WHERE node = 1;
SELECT node FROM t1;
}
} {}
do_test fkey2-4.3 {
execsql {
DELETE FROM t2 WHERE node = 1;
SELECT node FROM t2;
ROLLBACK;
}
} {4 5 6 7}
do_test fkey2-4.4 {
execsql { PRAGMA recursive_triggers = on }
execsql {
BEGIN;
DELETE FROM t1 WHERE node = 1;
SELECT node FROM t1;
}
} {}
do_test fkey2-4.3 {
execsql {
DELETE FROM t2 WHERE node = 1;
SELECT node FROM t2;
ROLLBACK;
}
} {}
#-------------------------------------------------------------------------
# Test cases fkey2-5.* verify that the incremental blob API may not
# write to a foreign key column while foreign-keys are enabled.
#
drop_all_tables
ifcapable incrblob {
do_test fkey2-5.1 {
execsql {
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1(a));
INSERT INTO t1 VALUES('hello', 'world');
INSERT INTO t2 VALUES('key', 'hello');
}
} {}
do_test fkey2-5.2 {
set rc [catch { set fd [db incrblob t2 b 1] } msg]
list $rc $msg
} {1 {cannot open foreign key column for writing}}
do_test fkey2-5.3 {
set rc [catch { set fd [db incrblob -readonly t2 b 1] } msg]
close $fd
set rc
} {0}
do_test fkey2-5.4 {
execsql { PRAGMA foreign_keys = off }
set rc [catch { set fd [db incrblob t2 b 1] } msg]
close $fd
set rc
} {0}
do_test fkey2-5.5 {
execsql { PRAGMA foreign_keys = on }
} {}
}
drop_all_tables
ifcapable vacuum {
do_test fkey2-6.1 {
execsql {
CREATE TABLE t1(a REFERENCES t2(c), b);
CREATE TABLE t2(c UNIQUE, b);
INSERT INTO t2 VALUES(1, 2);
INSERT INTO t1 VALUES(1, 2);
VACUUM;
}
} {}
}
#-------------------------------------------------------------------------
# Test that it is possible to use an INTEGER PRIMARY KEY as the child key
# of a foreign constraint.
#
drop_all_tables
do_test fkey2-7.1 {
execsql {
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE TABLE t2(c INTEGER PRIMARY KEY REFERENCES t1, b);
}
} {}
do_test fkey2-7.2 {
catchsql { INSERT INTO t2 VALUES(1, 'A'); }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-7.3 {
execsql {
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(2, 3);
INSERT INTO t2 VALUES(1, 'A');
}
} {}
do_test fkey2-7.4 {
execsql { UPDATE t2 SET c = 2 }
} {}
do_test fkey2-7.5 {
catchsql { UPDATE t2 SET c = 3 }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-7.6 {
catchsql { DELETE FROM t1 WHERE a = 2 }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-7.7 {
execsql { DELETE FROM t1 WHERE a = 1 }
} {}
do_test fkey2-7.8 {
catchsql { UPDATE t1 SET a = 3 }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-7.9 {
catchsql { UPDATE t2 SET rowid = 3 }
} {1 {FOREIGN KEY constraint failed}}
#-------------------------------------------------------------------------
# Test that it is not possible to enable/disable FK support while a
# transaction is open.
#
drop_all_tables
proc fkey2-8-test {tn zSql value} {
do_test fkey-2.8.$tn.1 [list execsql $zSql] {}
do_test fkey-2.8.$tn.2 { execsql "PRAGMA foreign_keys" } $value
}
fkey2-8-test 1 { PRAGMA foreign_keys = 0 } 0
fkey2-8-test 2 { PRAGMA foreign_keys = 1 } 1
fkey2-8-test 3 { BEGIN } 1
fkey2-8-test 4 { PRAGMA foreign_keys = 0 } 1
fkey2-8-test 5 { COMMIT } 1
fkey2-8-test 6 { PRAGMA foreign_keys = 0 } 0
fkey2-8-test 7 { BEGIN } 0
fkey2-8-test 8 { PRAGMA foreign_keys = 1 } 0
fkey2-8-test 9 { COMMIT } 0
fkey2-8-test 10 { PRAGMA foreign_keys = 1 } 1
fkey2-8-test 11 { PRAGMA foreign_keys = off } 0
fkey2-8-test 12 { PRAGMA foreign_keys = on } 1
fkey2-8-test 13 { PRAGMA foreign_keys = no } 0
fkey2-8-test 14 { PRAGMA foreign_keys = yes } 1
fkey2-8-test 15 { PRAGMA foreign_keys = false } 0
fkey2-8-test 16 { PRAGMA foreign_keys = true } 1
#-------------------------------------------------------------------------
# The following tests, fkey2-9.*, test SET DEFAULT actions.
#
drop_all_tables
do_test fkey2-9.1.1 {
execsql {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
CREATE TABLE t2(
c INTEGER PRIMARY KEY,
d INTEGER DEFAULT 1 REFERENCES t1 ON DELETE SET DEFAULT
);
DELETE FROM t1;
}
} {}
do_test fkey2-9.1.2 {
execsql {
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
INSERT INTO t2 VALUES(1, 2);
SELECT * FROM t2;
DELETE FROM t1 WHERE a = 2;
SELECT * FROM t2;
}
} {1 2 1 1}
do_test fkey2-9.1.3 {
execsql {
INSERT INTO t1 VALUES(2, 'two');
UPDATE t2 SET d = 2;
DELETE FROM t1 WHERE a = 1;
SELECT * FROM t2;
}
} {1 2}
do_test fkey2-9.1.4 {
execsql { SELECT * FROM t1 }
} {2 two}
do_test fkey2-9.1.5 {
catchsql { DELETE FROM t1 }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-9.2.1 {
execsql {
CREATE TABLE pp(a, b, c, PRIMARY KEY(b, c));
CREATE TABLE cc(d DEFAULT 3, e DEFAULT 1, f DEFAULT 2,
FOREIGN KEY(f, d) REFERENCES pp
ON UPDATE SET DEFAULT
ON DELETE SET NULL
);
INSERT INTO pp VALUES(1, 2, 3);
INSERT INTO pp VALUES(4, 5, 6);
INSERT INTO pp VALUES(7, 8, 9);
}
} {}
do_test fkey2-9.2.2 {
execsql {
INSERT INTO cc VALUES(6, 'A', 5);
INSERT INTO cc VALUES(6, 'B', 5);
INSERT INTO cc VALUES(9, 'A', 8);
INSERT INTO cc VALUES(9, 'B', 8);
UPDATE pp SET b = 1 WHERE a = 7;
SELECT * FROM cc;
}
} {6 A 5 6 B 5 3 A 2 3 B 2}
do_test fkey2-9.2.3 {
execsql {
DELETE FROM pp WHERE a = 4;
SELECT * FROM cc;
}
} {{} A {} {} B {} 3 A 2 3 B 2}
do_execsql_test fkey2-9.3.0 {
CREATE TABLE t3(x PRIMARY KEY REFERENCES t3 ON DELETE SET NULL);
INSERT INTO t3(x) VALUES(12345);
DROP TABLE t3;
} {}
#-------------------------------------------------------------------------
# The following tests, fkey2-10.*, test "foreign key mismatch" and
# other errors.
#
set tn 0
foreach zSql [list {
CREATE TABLE p(a PRIMARY KEY, b);
CREATE TABLE c(x REFERENCES p(c));
} {
CREATE TABLE c(x REFERENCES v(y));
CREATE VIEW v AS SELECT x AS y FROM c;
} {
CREATE TABLE p(a, b, PRIMARY KEY(a, b));
CREATE TABLE c(x REFERENCES p);
} {
CREATE TABLE p(a COLLATE binary, b);
CREATE UNIQUE INDEX i ON p(a COLLATE nocase);
CREATE TABLE c(x REFERENCES p(a));
}] {
drop_all_tables
do_test fkey2-10.1.[incr tn] {
execsql $zSql
catchsql { INSERT INTO c DEFAULT VALUES }
} {/1 {foreign key mismatch - "c" referencing "."}/}
}
# "rowid" cannot be used as part of a child or parent key definition
# unless it happens to be the name of an explicitly declared column.
#
do_test fkey2-10.2.1 {
drop_all_tables
catchsql {
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE TABLE t2(c, d, FOREIGN KEY(rowid) REFERENCES t1(a));
}
} {1 {unknown column "rowid" in foreign key definition}}
do_test fkey2-10.2.2 {
drop_all_tables
catchsql {
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE TABLE t2(rowid, d, FOREIGN KEY(rowid) REFERENCES t1(a));
}
} {0 {}}
do_test fkey2-10.2.1 {
drop_all_tables
catchsql {
CREATE TABLE t1(a, b);
CREATE TABLE t2(c, d, FOREIGN KEY(c) REFERENCES t1(rowid));
INSERT INTO t1(rowid, a, b) VALUES(1, 1, 1);
INSERT INTO t2 VALUES(1, 1);
}
} {1 {foreign key mismatch - "t2" referencing "t1"}}
do_test fkey2-10.2.2 {
drop_all_tables
catchsql {
CREATE TABLE t1(rowid PRIMARY KEY, b);
CREATE TABLE t2(c, d, FOREIGN KEY(c) REFERENCES t1(rowid));
INSERT INTO t1(rowid, b) VALUES(1, 1);
INSERT INTO t2 VALUES(1, 1);
}
} {0 {}}
#-------------------------------------------------------------------------
# The following tests, fkey2-11.*, test CASCADE actions.
#
drop_all_tables
do_test fkey2-11.1.1 {
execsql {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b, rowid, _rowid_, oid);
CREATE TABLE t2(c, d, FOREIGN KEY(c) REFERENCES t1(a) ON UPDATE CASCADE);
INSERT INTO t1 VALUES(10, 100, 'abc', 'def', 'ghi');
INSERT INTO t2 VALUES(10, 100);
UPDATE t1 SET a = 15;
SELECT * FROM t2;
}
} {15 100}
#-------------------------------------------------------------------------
# The following tests, fkey2-12.*, test RESTRICT actions.
#
drop_all_tables
do_test fkey2-12.1.1 {
execsql {
CREATE TABLE t1(a, b PRIMARY KEY);
CREATE TABLE t2(
x REFERENCES t1 ON UPDATE RESTRICT DEFERRABLE INITIALLY DEFERRED
);
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
INSERT INTO t1 VALUES(3, 'three');
}
} {}
do_test fkey2-12.1.2 {
execsql "BEGIN"
execsql "INSERT INTO t2 VALUES('two')"
} {}
do_test fkey2-12.1.3 {
execsql "UPDATE t1 SET b = 'four' WHERE b = 'one'"
} {}
do_test fkey2-12.1.4 {
catchsql "UPDATE t1 SET b = 'five' WHERE b = 'two'"
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-12.1.5 {
execsql "DELETE FROM t1 WHERE b = 'two'"
} {}
do_test fkey2-12.1.6 {
catchsql "COMMIT"
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-12.1.7 {
execsql {
INSERT INTO t1 VALUES(2, 'two');
COMMIT;
}
} {}
drop_all_tables
do_test fkey2-12.2.1 {
execsql {
CREATE TABLE t1(x COLLATE NOCASE PRIMARY KEY);
CREATE TRIGGER tt1 AFTER DELETE ON t1
WHEN EXISTS ( SELECT 1 FROM t2 WHERE old.x = y )
BEGIN
INSERT INTO t1 VALUES(old.x);
END;
CREATE TABLE t2(y REFERENCES t1);
INSERT INTO t1 VALUES('A');
INSERT INTO t1 VALUES('B');
INSERT INTO t2 VALUES('a');
INSERT INTO t2 VALUES('b');
SELECT * FROM t1;
SELECT * FROM t2;
}
} {A B a b}
do_test fkey2-12.2.2 {
execsql { DELETE FROM t1 }
execsql {
SELECT * FROM t1;
SELECT * FROM t2;
}
} {A B a b}
do_test fkey2-12.2.3 {
execsql {
DROP TABLE t2;
CREATE TABLE t2(y REFERENCES t1 ON DELETE RESTRICT);
INSERT INTO t2 VALUES('a');
INSERT INTO t2 VALUES('b');
}
catchsql { DELETE FROM t1 }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-12.2.4 {
execsql {
SELECT * FROM t1;
SELECT * FROM t2;
}
} {A B a b}
drop_all_tables
do_test fkey2-12.3.1 {
execsql {
CREATE TABLE up(
c00, c01, c02, c03, c04, c05, c06, c07, c08, c09,
c10, c11, c12, c13, c14, c15, c16, c17, c18, c19,
c20, c21, c22, c23, c24, c25, c26, c27, c28, c29,
c30, c31, c32, c33, c34, c35, c36, c37, c38, c39,
PRIMARY KEY(c34, c35)
);
CREATE TABLE down(
c00, c01, c02, c03, c04, c05, c06, c07, c08, c09,
c10, c11, c12, c13, c14, c15, c16, c17, c18, c19,
c20, c21, c22, c23, c24, c25, c26, c27, c28, c29,
c30, c31, c32, c33, c34, c35, c36, c37, c38, c39,
FOREIGN KEY(c39, c38) REFERENCES up ON UPDATE CASCADE
);
}
} {}
do_test fkey2-12.3.2 {
execsql {
INSERT INTO up(c34, c35) VALUES('yes', 'no');
INSERT INTO down(c39, c38) VALUES('yes', 'no');
UPDATE up SET c34 = 'possibly';
SELECT c38, c39 FROM down;
DELETE FROM down;
}
} {no possibly}
do_test fkey2-12.3.3 {
catchsql { INSERT INTO down(c39, c38) VALUES('yes', 'no') }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-12.3.4 {
execsql {
INSERT INTO up(c34, c35) VALUES('yes', 'no');
INSERT INTO down(c39, c38) VALUES('yes', 'no');
}
catchsql { DELETE FROM up WHERE c34 = 'yes' }
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-12.3.5 {
execsql {
DELETE FROM up WHERE c34 = 'possibly';
SELECT c34, c35 FROM up;
SELECT c39, c38 FROM down;
}
} {yes no yes no}
#-------------------------------------------------------------------------
# The following tests, fkey2-13.*, test that FK processing is performed
# when rows are REPLACEd.
#
drop_all_tables
do_test fkey2-13.1.1 {
execsql {
CREATE TABLE pp(a UNIQUE, b, c, PRIMARY KEY(b, c));
CREATE TABLE cc(d, e, f UNIQUE, FOREIGN KEY(d, e) REFERENCES pp);
INSERT INTO pp VALUES(1, 2, 3);
INSERT INTO cc VALUES(2, 3, 1);
}
} {}
foreach {tn stmt} {
1 "REPLACE INTO pp VALUES(1, 4, 5)"
2 "REPLACE INTO pp(rowid, a, b, c) VALUES(1, 2, 3, 4)"
} {
do_test fkey2-13.1.$tn.1 {
catchsql $stmt
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-13.1.$tn.2 {
execsql {
SELECT * FROM pp;
SELECT * FROM cc;
}
} {1 2 3 2 3 1}
do_test fkey2-13.1.$tn.3 {
execsql BEGIN;
catchsql $stmt
} {1 {FOREIGN KEY constraint failed}}
do_test fkey2-13.1.$tn.4 {
execsql {
COMMIT;
SELECT * FROM pp;
SELECT * FROM cc;
}
} {1 2 3 2 3 1}
}
do_test fkey2-13.1.3 {
execsql {
REPLACE INTO pp(rowid, a, b, c) VALUES(1, 2, 2, 3);
SELECT rowid, * FROM pp;
SELECT * FROM cc;
}
} {1 2 2 3 2 3 1}
do_test fkey2-13.1.4 {
execsql {
REPLACE INTO pp(rowid, a, b, c) VALUES(2, 2, 2, 3);
SELECT rowid, * FROM pp;
SELECT * FROM cc;
}
} {2 2 2 3 2 3 1}
#-------------------------------------------------------------------------
# The following tests, fkey2-14.*, test that the "DROP TABLE" and "ALTER
# TABLE" commands work as expected wrt foreign key constraints.
#
# fkey2-14.1*: ALTER TABLE ADD COLUMN
# fkey2-14.2*: ALTER TABLE RENAME TABLE
# fkey2-14.3*: DROP TABLE
#
drop_all_tables
ifcapable altertable {
do_test fkey2-14.1.1 {
# Adding a column with a REFERENCES clause is not supported.
execsql {
CREATE TABLE t1(a PRIMARY KEY);
CREATE TABLE t2(a, b);
INSERT INTO t2 VALUES(1,2);
}
catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 }
} {0 {}}
do_test fkey2-14.1.2 {
catchsql { ALTER TABLE t2 ADD COLUMN d DEFAULT NULL REFERENCES t1 }
} {0 {}}
do_test fkey2-14.1.3 {
catchsql { ALTER TABLE t2 ADD COLUMN e REFERENCES t1 DEFAULT NULL}
} {0 {}}
do_test fkey2-14.1.4 {
catchsql { ALTER TABLE t2 ADD COLUMN f REFERENCES t1 DEFAULT 'text'}
} {1 {Cannot add a REFERENCES column with non-NULL default value}}
do_test fkey2-14.1.5 {
catchsql { ALTER TABLE t2 ADD COLUMN g DEFAULT CURRENT_TIME REFERENCES t1 }
} {1 {Cannot add a REFERENCES column with non-NULL default value}}
do_test fkey2-14.1.6 {
execsql {
PRAGMA foreign_keys = off;
ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1;
PRAGMA foreign_keys = on;
SELECT sql FROM sqlite_master WHERE name='t2';
}
} {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}}
# Test the sqlite_rename_parent() function directly.
#
proc test_rename_parent {zCreate zOld zNew} {
db eval {SELECT sqlite_rename_table(
'main', 'table', 't1', $zCreate, $zOld, $zNew, 0
)}
}
sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
do_test fkey2-14.2.1.1 {
test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3
} {{CREATE TABLE t1(a REFERENCES "t3")}}
do_test fkey2-14.2.1.2 {
test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3
} {{CREATE TABLE t1(a REFERENCES t2)}}
do_test fkey2-14.2.1.3 {
test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3
} {{CREATE TABLE t1(a REFERENCES "t3")}}