Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/conflict4.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / conflict4.test` 162 lines ยท 4746 bytes Raw Blame History 1 # 2026-07-20 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 # 12 # Test cases for UNIQUE ON CONFLICT REPLACE and UNIQUE ON CONFLICT FAIL 13 # behavior including when there are also triggers and/or an UPSERT. 14 # 15 # Forum thread 2026-07-17T09:30:42Z 16 # 17 set testdir [file dirname $argv0] 18 source $testdir/tester.tcl 19 set testprefix conflict4 20 21 # Combination of ON CONFLICT FAIL on a UNIQUE index and 22 # an ON CONFLICT DO NOTHING in a different index leads to 23 # database corruption. 24 # 25 do_execsql_test 1.1 { 26 CREATE TABLE t0( 27 a INTEGER PRIMARY KEY ON CONFLICT REPLACE, 28 b INT UNIQUE ON CONFLICT FAIL, 29 c INT UNIQUE ON CONFLICT ABORT 30 ); 31 INSERT INTO t0 VALUES (1, 10, 100),(2, 20, 200); 32 } 33 do_catchsql_test 1.2 { 34 INSERT INTO t0 VALUES (1, 20, 300) ON CONFLICT(c) DO NOTHING; 35 } {1 {UNIQUE constraint failed: t0.b}} 36 do_execsql_test 1.3 { 37 PRAGMA integrity_check; 38 } {ok} 39 do_execsql_test 1.4 { 40 SELECT * FROM t0 ORDER BY a; 41 } {1 10 100 2 20 200} 42 do_execsql_test 1.5 { 43 DROP TABLE t0; 44 CREATE TABLE t0( 45 a INTEGER PRIMARY KEY ON CONFLICT REPLACE, 46 b INT UNIQUE ON CONFLICT IGNORE, 47 c INT UNIQUE ON CONFLICT ABORT 48 ); 49 INSERT INTO t0 VALUES (1, 10, 100),(2, 20, 200); 50 } 51 do_catchsql_test 1.6 { 52 INSERT INTO t0 VALUES (1, 20, 300) ON CONFLICT(c) DO NOTHING; 53 } {0 {}} 54 do_execsql_test 1.7 { 55 PRAGMA integrity_check; 56 } {ok} 57 do_execsql_test 1.8 { 58 SELECT * FROM t0 ORDER BY a; 59 } {1 10 100 2 20 200} 60 do_execsql_test 1.9 { 61 DROP TABLE t0; 62 CREATE TABLE t0( 63 a INTEGER PRIMARY KEY ON CONFLICT REPLACE, 64 b INT UNIQUE ON CONFLICT FAIL, 65 c INT UNIQUE ON CONFLICT ABORT 66 ); 67 INSERT INTO t0 VALUES (1, 10, 100),(2, 20, 200); 68 } 69 do_catchsql_test 1.10 { 70 INSERT INTO t0 VALUES (1, 20, 300) ON CONFLICT(c) DO NOTHING; 71 } {1 {UNIQUE constraint failed: t0.b}} 72 do_execsql_test 1.11 { 73 PRAGMA integrity_check; 74 } {ok} 75 do_execsql_test 1.12 { 76 SELECT * FROM t0 ORDER BY a; 77 } {1 10 100 2 20 200} 78 do_execsql_test 1.13 { 79 DROP TABLE t0; 80 CREATE TABLE t1( 81 a INTEGER PRIMARY KEY, 82 b UNIQUE ON CONFLICT FAIL, 83 c UNIQUE 84 ); 85 INSERT INTO t1 VALUES(1, 1, 1); 86 } 87 do_catchsql_test 1.14 { 88 -- Inserts 0 rows, because hitting the constraint on (b) does ABORT: 89 INSERT INTO t1 VALUES(3, 3, 3), (2, 1, 2) ON CONFLICT(c) DO NOTHING; 90 } {1 {UNIQUE constraint failed: t1.b}} 91 do_execsql_test 1.15 { 92 SELECT * FROM t1 ORDER BY a; 93 } {1 1 1 3 3 3} 94 do_catchsql_test 1.16 { 95 -- Inserts 1 row, because hitting the constraint on (b) does FAIL: 96 DELETE FROM t1 WHERE a=3; 97 INSERT INTO t1 VALUES(3, 3, 3), (2, 1, 2); 98 } {1 {UNIQUE constraint failed: t1.b}} 99 do_execsql_test 1.17 { 100 SELECT * FROM t1 ORDER BY a; 101 } {1 1 1 3 3 3} 102 do_execsql_test 1.18 { 103 PRAGMA integrity_check; 104 } ok 105 106 # Counter-example to the fix at check-in 2026-07-17T16:26:56.257Z 107 # provided by Pavan Nambi at forum post 2026-07-18T03:35:48Z 108 do_execsql_test 2.1 { 109 DROP TABLE t1; 110 PRAGMA recursive_triggers=ON; 111 CREATE TABLE t2( 112 a INTEGER PRIMARY KEY ON CONFLICT REPLACE, 113 b INT UNIQUE ON CONFLICT FAIL, 114 c INT UNIQUE ON CONFLICT ABORT 115 ); 116 CREATE TRIGGER t_ad AFTER DELETE ON t2 BEGIN 117 INSERT INTO t2 VALUES(99,30,999); 118 END; 119 INSERT INTO t2 VALUES(1,10,100),(2,20,200); 120 SELECT * FROM t2 ORDER BY a; 121 } {1 10 100 2 20 200} 122 do_catchsql_test 2.2 { 123 INSERT INTO t2 VALUES(4,40,400),(1,30,300),(5,50,500) 124 ON CONFLICT(c) DO NOTHING; 125 } {1 {UNIQUE constraint failed: t2.b}} 126 do_execsql_test 2.3 { 127 PRAGMA integrity_check; 128 } ok 129 do_execsql_test 2.4 { 130 SELECT * FROM t2 ORDER BY a; 131 } {1 10 100 2 20 200} 132 133 # This is a slightly simpler version of the test above. Before 134 # the bug was fixed, this would: 135 # 136 # 1) Checks there are no instances of 'HELLO' in the index on t1(b), 137 # 2) Deletes the row from t1 as a result of the REPLACE on the PK, 138 # 3) Fires the trigger, inserting ('HELLO', 200) into index t1(b). 139 # 4) Finishes the insert without rechecking t1(b), inserting 140 # ('HELLO', 100). Corruption. 141 # 142 reset_db 143 do_execsql_test 3.1 { 144 PRAGMA recursive_triggers = ON; 145 CREATE TABLE t1( 146 a PRIMARY KEY ON CONFLICT REPLACE, 147 b UNIQUE 148 ); 149 INSERT INTO t1 VALUES(100, 'abcde'); 150 CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN 151 INSERT INTO t1 VALUES(200, 'HELLO'); 152 END; 153 } 154 do_catchsql_test 3.2 { 155 INSERT INTO t1 VALUES(100, 'HELLO'); 156 } {1 {UNIQUE constraint failed: t1.b}} 157 do_execsql_test 3.3 { 158 PRAGMA integrity_check; 159 SELECT * FROM t1 ORDER BY +a; 160 } {ok 100 abcde} 161 162 finish_test