fkey3.test 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # 2009 September 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  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.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library.
  12. #
  13. # This file implements tests for foreign keys.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable {!foreignkey||!trigger} {
  18. finish_test
  19. return
  20. }
  21. set testprefix fkey3
  22. # Create a table and some data to work with.
  23. #
  24. do_test fkey3-1.1 {
  25. execsql {
  26. PRAGMA foreign_keys=ON;
  27. CREATE TABLE t1(x INTEGER PRIMARY KEY);
  28. INSERT INTO t1 VALUES(100);
  29. INSERT INTO t1 VALUES(101);
  30. CREATE TABLE t2(y INTEGER REFERENCES t1 (x));
  31. INSERT INTO t2 VALUES(100);
  32. INSERT INTO t2 VALUES(101);
  33. SELECT 1, x FROM t1;
  34. SELECT 2, y FROM t2;
  35. }
  36. } {1 100 1 101 2 100 2 101}
  37. do_test fkey3-1.2 {
  38. catchsql {
  39. DELETE FROM t1 WHERE x=100;
  40. }
  41. } {1 {foreign key constraint failed}}
  42. do_test fkey3-1.3 {
  43. catchsql {
  44. DROP TABLE t1;
  45. }
  46. } {1 {foreign key constraint failed}}
  47. do_test fkey3-1.4 {
  48. execsql {
  49. DROP TABLE t2;
  50. }
  51. } {}
  52. do_test fkey3-1.5 {
  53. execsql {
  54. DROP TABLE t1;
  55. }
  56. } {}
  57. do_test fkey3-2.1 {
  58. execsql {
  59. PRAGMA foreign_keys=ON;
  60. CREATE TABLE t1(x INTEGER PRIMARY KEY);
  61. INSERT INTO t1 VALUES(100);
  62. INSERT INTO t1 VALUES(101);
  63. CREATE TABLE t2(y INTEGER PRIMARY KEY REFERENCES t1 (x) ON UPDATE SET NULL);
  64. }
  65. execsql {
  66. INSERT INTO t2 VALUES(100);
  67. INSERT INTO t2 VALUES(101);
  68. SELECT 1, x FROM t1;
  69. SELECT 2, y FROM t2;
  70. }
  71. } {1 100 1 101 2 100 2 101}
  72. #-------------------------------------------------------------------------
  73. # The following tests - fkey-3.* - test some edge cases to do with
  74. # inserting rows into tables that have foreign keys where the parent
  75. # table is the same as the child table. Especially cases where the
  76. # new row being inserted matches itself.
  77. #
  78. do_execsql_test 3.1.1 {
  79. CREATE TABLE t3(a, b, c, d,
  80. UNIQUE(a, b),
  81. FOREIGN KEY(c, d) REFERENCES t3(a, b)
  82. );
  83. INSERT INTO t3 VALUES(1, 2, 1, 2);
  84. } {}
  85. do_catchsql_test 3.1.2 {
  86. INSERT INTO t3 VALUES(NULL, 2, 5, 2);
  87. } {1 {foreign key constraint failed}}
  88. do_catchsql_test 3.1.3 {
  89. INSERT INTO t3 VALUES(NULL, 3, 5, 2);
  90. } {1 {foreign key constraint failed}}
  91. do_execsql_test 3.2.1 {
  92. CREATE TABLE t4(a UNIQUE, b REFERENCES t4(a));
  93. }
  94. do_catchsql_test 3.2.2 {
  95. INSERT INTO t4 VALUES(NULL, 1);
  96. } {1 {foreign key constraint failed}}
  97. do_execsql_test 3.3.1 {
  98. CREATE TABLE t5(a INTEGER PRIMARY KEY, b REFERENCES t5(a));
  99. INSERT INTO t5 VALUES(NULL, 1);
  100. } {}
  101. do_catchsql_test 3.3.2 {
  102. INSERT INTO t5 VALUES(NULL, 3);
  103. } {1 {foreign key constraint failed}}
  104. do_execsql_test 3.4.1 {
  105. CREATE TABLE t6(a INTEGER PRIMARY KEY, b, c, d,
  106. FOREIGN KEY(c, d) REFERENCES t6(a, b)
  107. );
  108. CREATE UNIQUE INDEX t6i ON t6(b, a);
  109. }
  110. do_execsql_test 3.4.2 { INSERT INTO t6 VALUES(NULL, 'a', 1, 'a'); } {}
  111. do_execsql_test 3.4.3 { INSERT INTO t6 VALUES(2, 'a', 2, 'a'); } {}
  112. do_execsql_test 3.4.4 { INSERT INTO t6 VALUES(NULL, 'a', 1, 'a'); } {}
  113. do_execsql_test 3.4.5 { INSERT INTO t6 VALUES(5, 'a', 2, 'a'); } {}
  114. do_catchsql_test 3.4.6 {
  115. INSERT INTO t6 VALUES(NULL, 'a', 65, 'a');
  116. } {1 {foreign key constraint failed}}
  117. do_execsql_test 3.4.7 {
  118. INSERT INTO t6 VALUES(100, 'one', 100, 'one');
  119. DELETE FROM t6 WHERE a = 100;
  120. }
  121. do_execsql_test 3.4.8 {
  122. INSERT INTO t6 VALUES(100, 'one', 100, 'one');
  123. UPDATE t6 SET c = 1, d = 'a' WHERE a = 100;
  124. DELETE FROM t6 WHERE a = 100;
  125. }
  126. do_execsql_test 3.5.1 {
  127. CREATE TABLE t7(a, b, c, d INTEGER PRIMARY KEY,
  128. FOREIGN KEY(c, d) REFERENCES t7(a, b)
  129. );
  130. CREATE UNIQUE INDEX t7i ON t7(a, b);
  131. }
  132. do_execsql_test 3.5.2 { INSERT INTO t7 VALUES('x', 1, 'x', NULL) } {}
  133. do_execsql_test 3.5.3 { INSERT INTO t7 VALUES('x', 2, 'x', 2) } {}
  134. do_catchsql_test 3.5.4 {
  135. INSERT INTO t7 VALUES('x', 450, 'x', NULL);
  136. } {1 {foreign key constraint failed}}
  137. do_catchsql_test 3.5.5 {
  138. INSERT INTO t7 VALUES('x', 450, 'x', 451);
  139. } {1 {foreign key constraint failed}}
  140. do_execsql_test 3.6.1 {
  141. CREATE TABLE t8(a, b, c, d, e, FOREIGN KEY(c, d) REFERENCES t8(a, b));
  142. CREATE UNIQUE INDEX t8i1 ON t8(a, b);
  143. CREATE UNIQUE INDEX t8i2 ON t8(c);
  144. INSERT INTO t8 VALUES(1, 1, 1, 1, 1);
  145. }
  146. do_catchsql_test 3.6.2 {
  147. UPDATE t8 SET d = 2;
  148. } {1 {foreign key constraint failed}}
  149. do_execsql_test 3.6.3 { UPDATE t8 SET d = 1; }
  150. do_execsql_test 3.6.4 { UPDATE t8 SET e = 2; }
  151. do_catchsql_test 3.6.5 {
  152. CREATE TABLE TestTable (
  153. id INTEGER PRIMARY KEY,
  154. name text,
  155. source_id integer not null,
  156. parent_id integer,
  157. foreign key(source_id, parent_id) references TestTable(source_id, id)
  158. );
  159. CREATE UNIQUE INDEX testindex on TestTable(source_id, id);
  160. PRAGMA foreign_keys=1;
  161. INSERT INTO TestTable VALUES (1, 'parent', 1, null);
  162. INSERT INTO TestTable VALUES (2, 'child', 1, 1);
  163. UPDATE TestTable SET parent_id=1000 where id=2;
  164. } {1 {foreign key constraint failed}}
  165. finish_test