]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Enhance the error message for "foreign key mismatch" to include the names of
authordrh <drh@noemail.net>
Mon, 17 Dec 2012 20:40:39 +0000 (20:40 +0000)
committerdrh <drh@noemail.net>
Mon, 17 Dec 2012 20:40:39 +0000 (20:40 +0000)
the child and parent tables.  Begin adding test cases for
PRAGMA foreign_key_check.  Make sure PRAGMA foreign_key_check gets all
necessary table locks.

FossilOrigin-Name: 0f9963526c299757ff95c153fb05a67cddb6af28

manifest
manifest.uuid
src/fkey.c
src/pragma.c
test/e_fkey.test
test/fkey2.test

index 40430b8c8a9089f6c79d6a5e27f65908c51bb4da..7dfdfb2ad378ebc79238f31e44a6466dc57bfa69 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C "PRAGMA\sforeign_key_check"\swith\sno\sargument\schecks\sthe\sforeign\skeys\son\sall\ntables.
-D 2012-12-17T18:43:02.925
+C Enhance\sthe\serror\smessage\sfor\s"foreign\skey\smismatch"\sto\sinclude\sthe\snames\sof\nthe\schild\sand\sparent\stables.\s\sBegin\sadding\stest\scases\sfor\s\nPRAGMA\sforeign_key_check.\s\sMake\ssure\sPRAGMA\sforeign_key_check\sgets\sall\nnecessary\stable\slocks.
+D 2012-12-17T20:40:39.698
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 690d441a758cbffd13e814dc2724a721a6ebd400
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -132,7 +132,7 @@ F src/date.c 067a81c9942c497aafd2c260e13add8a7d0c7dd4
 F src/delete.c 9b8d308979114991e5dc7cee958316e07186941d
 F src/expr.c 0e41d66d868b37dbc0e041c342e0036fad27e705
 F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
-F src/fkey.c 21fb59849f5ce409d82736a34c1c21612d029e59
+F src/fkey.c 5b7a12e2f8620e855b0478a9a6798df9967bb277
 F src/func.c 8147799b048065a1590805be464d05b4913e652c
 F src/global.c e59ecd2c553ad0d4bfbc84ca71231336f8993a7a
 F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4
@@ -168,7 +168,7 @@ F src/parse.y c2b4a6454ad77299b1443e2c483a560a9f16e724
 F src/pcache.c f8043b433a57aba85384a531e3937a804432a346
 F src/pcache.h 1b5dcc3dc8103d03e625b177023ee67764fa6b7c
 F src/pcache1.c 9fd22671c270b35131ef480bbc00392b8b5f8ab9
-F src/pragma.c b98dfe89b961d41be6d502d23adf18ed02dbb410
+F src/pragma.c 5cc87af77dd57e037258410e0ee5a9dab51a8a5a
 F src/prepare.c 931ad0d852a0df48f79adcba6ce79ca5f475625c
 F src/printf.c 4a9f882f1c1787a8b494a2987765acf9d97ac21f
 F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
@@ -383,7 +383,7 @@ F test/e_delete.test 89aa84d3d1bd284a0689ede04bce10226a5aeaa5
 F test/e_droptrigger.test afd5c4d27dec607f5997a66bf7e2498a082cb235
 F test/e_dropview.test 583411e470458c5d76148542cfb5a5fa84c8f93e
 F test/e_expr.test 5489424d3d9a452ac3701cdf4b680ae31a157894
-F test/e_fkey.test a79ab1d3213c7ac64621eec28f8e8bb219775445
+F test/e_fkey.test 89a2ff734a33693b997534eff90724573948be7b
 F test/e_fts3.test 5c02288842e4f941896fd44afdef564dd5fc1459
 F test/e_insert.test d5331cc95e101af2508159fc98b6801631659ffe
 F test/e_reindex.test dfedfc32c5a282b0596c6537cbcd4217fbb1a216
@@ -409,7 +409,7 @@ F test/fallocate.test b5d34437bd7ab01d41b1464b8117aefd4d32160e
 F test/filectrl.test 14fa712e42c4cb791e09dfd58a6a03efb47ef13a
 F test/filefmt.test ffa17b5aebc3eb4b1e3be1ccb5ee906ffbd97f6e
 F test/fkey1.test 01c7de578e11747e720c2d9aeef27f239853c4da
-F test/fkey2.test 080969fe219b3b082b0e097ac18c6af2e5b0631f
+F test/fkey2.test 5aa44e7153928a1f002803f94aaab4c76a7ceac2
 F test/fkey3.test 5ec899d12b13bcf1e9ef40eff7fb692fdb91392e
 F test/fkey4.test c6c8f9f9be885f95c85c7bceb26f243ad906fd49
 F test/fkey_malloc.test c3a12acd053c976de09036498eef09b83afa4a80
@@ -1025,7 +1025,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P 603b695bf094b9da905f877a13dcb134463db92a
-R 838d64421c46ea50daf2c57db5adcb2d
+P 57d47423a65d183cb1efcb95b28cb143e788b953
+R 48149676f68d877f18e0c8a269ce004d
 U drh
-Z f327b298aa59d7b1bf00c17e68686c2c
+Z 4108a840f9ef999ff91109d05e04ab7d
index a607adedca33158d74599689731802dbb2851c02..02f0c8f087636d74c877aa1bd26d67864b8b7117 100644 (file)
@@ -1 +1 @@
-57d47423a65d183cb1efcb95b28cb143e788b953
\ No newline at end of file
+0f9963526c299757ff95c153fb05a67cddb6af28
\ No newline at end of file
index c9b7c51af95d2a25566a15dd5d3d4fb79e1a0a8b..421311dc3562ad55f240ac3f2fdfe6de60c92124 100644 (file)
@@ -275,7 +275,9 @@ int sqlite3FkLocateIndex(
 
   if( !pIdx ){
     if( !pParse->disableTriggers ){
-      sqlite3ErrorMsg(pParse, "foreign key mismatch");
+      sqlite3ErrorMsg(pParse,
+           "foreign key mismatch - \"%w\" referencing \"%w\"",
+           pFKey->pFrom->zName, pFKey->zTo);
     }
     sqlite3DbFree(pParse->db, aiCol);
     return 1;
index d65f4cd10119db43d4b586b2f7630b222f49fbc9..60e91e6733d8832ff17203112170343b91f582aa 100644 (file)
@@ -1150,7 +1150,8 @@ void sqlite3Pragma(
         pTab = (Table*)sqliteHashData(k);
         k = sqliteHashNext(k);
       }
-      if( pTab->pFKey==0 ) continue;
+      if( pTab==0 || pTab->pFKey==0 ) continue;
+      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
       sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
       sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
@@ -1159,6 +1160,7 @@ void sqlite3Pragma(
         pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
         if( pParent==0 ) break;
         pIdx = 0;
+        sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
         x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
         if( x==0 ){
           if( pIdx==0 ){
index 69e086864299e2c59aac704a9e1997166cc7aa17..01bf53452707d4961bbbf42ca8a05e05f235f505 100644 (file)
@@ -627,7 +627,8 @@ proc test_efkey_57 {tn isError sql} {
   execsql $sql
   do_test e_fkey-18.$tn {
     catchsql { INSERT INTO t2 VALUES(NULL) }
-  } [lindex {{0 {}} {1 {foreign key mismatch}}} $isError]
+  } [lindex {{0 {}} {/1 {foreign key mismatch - ".*" referencing ".*"}/}} \
+     $isError]
 }
 test_efkey_57 2 0 { CREATE TABLE t1(x PRIMARY KEY) }
 test_efkey_57 3 0 { CREATE TABLE t1(x UNIQUE) }
@@ -698,16 +699,16 @@ do_test e_fkey-19.2 {
 } {}
 do_test e_fkey-19.2 {
   catchsql { INSERT INTO child4 VALUES('xxx', 5) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child4" referencing "parent"}}
 do_test e_fkey-19.3 {
   catchsql { INSERT INTO child5 VALUES('xxx', 6) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child5" referencing "parent"}}
 do_test e_fkey-19.4 {
   catchsql { INSERT INTO child6 VALUES(2, 3) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child6" referencing "parent"}}
 do_test e_fkey-19.5 {
   catchsql { INSERT INTO child7 VALUES(3) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child7" referencing "parent"}}
 
 #-------------------------------------------------------------------------
 # Test errors in the database schema that are detected while preparing
@@ -765,12 +766,12 @@ do_test e_fkey-20.1 {
 
 foreach {tn tbl ptbl err} {
   2 c1 {} "no such table: main.nosuchtable"
-  3 c2 p2 "foreign key mismatch"
-  4 c3 p3 "foreign key mismatch"
-  5 c4 p4 "foreign key mismatch"
-  6 c5 p5 "foreign key mismatch"
-  7 c6 p6 "foreign key mismatch"
-  8 c7 p7 "foreign key mismatch"
+  3 c2 p2 "foreign key mismatch - \"c2\" referencing \"p2\""
+  4 c3 p3 "foreign key mismatch - \"c3\" referencing \"p3\""
+  5 c4 p4 "foreign key mismatch - \"c4\" referencing \"p4\""
+  6 c5 p5 "foreign key mismatch - \"c5\" referencing \"p5\""
+  7 c6 p6 "foreign key mismatch - \"c6\" referencing \"p6\""
+  8 c7 p7 "foreign key mismatch - \"c7\" referencing \"p7\""
 } {
   do_test e_fkey-20.$tn.1 {
     catchsql "INSERT INTO $tbl VALUES('a', 'b')"
@@ -820,22 +821,22 @@ do_test e_fkey-21.2 {
 } {}
 do_test e_fkey-21.3 {
   catchsql { INSERT INTO child9 VALUES('I') }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child9" referencing "parent2"}}
 do_test e_fkey-21.4 {
   catchsql { INSERT INTO child9 VALUES('II') }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child9" referencing "parent2"}}
 do_test e_fkey-21.5 {
   catchsql { INSERT INTO child9 VALUES(NULL) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child9" referencing "parent2"}}
 do_test e_fkey-21.6 {
   catchsql { INSERT INTO child10 VALUES('I', 'II', 'III') }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child10" referencing "parent2"}}
 do_test e_fkey-21.7 {
   catchsql { INSERT INTO child10 VALUES(1, 2, 3) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child10" referencing "parent2"}}
 do_test e_fkey-21.8 {
   catchsql { INSERT INTO child10 VALUES(NULL, NULL, NULL) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child10" referencing "parent2"}}
 
 #-------------------------------------------------------------------------
 # Test errors that are reported when creating the child table. 
@@ -1151,7 +1152,7 @@ do_test e_fkey-28.8 {
     CREATE TABLE c(a, b, FOREIGN KEY(a,b) REFERENCES p);
   }
   catchsql {DELETE FROM p}
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c" referencing "p"}}
 do_test e_fkey-28.9 {
   drop_all_tables
   execsql {
@@ -1159,7 +1160,7 @@ do_test e_fkey-28.9 {
     CREATE TABLE c(a REFERENCES p);
   }
   catchsql {DELETE FROM p}
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c" referencing "p"}}
 
 
 #-------------------------------------------------------------------------
@@ -2729,11 +2730,11 @@ do_test e_fkey-60.3 {
 do_test e_fkey-60.4 {
   execsql { CREATE TABLE nosuchtable(x PRIMARY KEY) }
   catchsql { DELETE FROM p }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c2" referencing "p"}}
 do_test e_fkey-60.5 {
   execsql { DROP TABLE c1 }
   catchsql { DELETE FROM p }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c2" referencing "p"}}
 do_test e_fkey-60.6 {
   execsql { DROP TABLE c2 }
   execsql { DELETE FROM p }
index f0cc4d244cba17e61a1378d4d2d7ba5e62cfcaf1..a7235987884beca9b0107ddb0b2009eda631b1d3 100644 (file)
@@ -139,14 +139,21 @@ set FkeySimpleTests {
   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}}
+  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 { catchsql $zSql } $res
+  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
 
@@ -155,6 +162,12 @@ do_test fkey2-1.2.0 {
 } {}
 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
 
@@ -165,6 +178,12 @@ do_test fkey2-1.3.0 {
 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
@@ -681,7 +700,7 @@ foreach zSql [list {
   do_test fkey2-10.1.[incr tn] {
     execsql $zSql
     catchsql { INSERT INTO c DEFAULT VALUES }
-  } {1 {foreign key mismatch}}
+  } {/1 {foreign key mismatch - "c" referencing "."}/}
 }
 
 # "rowid" cannot be used as part of a child or parent key definition 
@@ -709,7 +728,7 @@ do_test fkey2-10.2.1 {
     INSERT INTO t1(rowid, a, b) VALUES(1, 1, 1);
     INSERT INTO t2 VALUES(1, 1);
   }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "t2" referencing "t1"}}
 do_test fkey2-10.2.2 {
   drop_all_tables
   catchsql {
@@ -1223,7 +1242,7 @@ do_test fkey-2.14.3.8 {
     CREATE TABLE cc(a, b, FOREIGN KEY(a, b) REFERENCES pp(x, z));
   }
   catchsql { INSERT INTO cc VALUES(1, 2) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "cc" referencing "pp"}}
 do_test fkey-2.14.3.9 {
   execsql { DROP TABLE cc }
 } {}