Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Many new test cases added, that mostly work. Currently 18 errors in without_rowid3.test. Also there is a hack marked by a /*FIXME*/ comment on at fkey.c:547 that needs fixing. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | omit-rowid |
Files: | files | file ages | folders |
SHA1: |
39e32187b66405e00dbd44685f6cdd26 |
User & Date: | drh 2013-11-02 22:09:48 |
Context
2013-11-02
| ||
22:29 | Improved Synopsis on register comparison operators. Fix a bug on the constraint check generator. (check-in: a7a18b65 user: drh tags: omit-rowid) | |
22:09 | Many new test cases added, that mostly work. Currently 18 errors in without_rowid3.test. Also there is a hack marked by a /*FIXME*/ comment on at fkey.c:547 that needs fixing. (check-in: 39e32187 user: drh tags: omit-rowid) | |
19:34 | Change the "idx" name of the primary key index for WITHOUT ROWID tables in sqlite_statN statistics tables to be the name of the table rather than the fabricated index name (ex: sqlite_autoindex_xyz_1). This makes it consistent with sqlite_master table. (check-in: 4ee4d310 user: drh tags: omit-rowid) | |
Changes
Changes to src/build.c.
︙ | ︙ | |||
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 | if( pList==0 ) return; pList->a[0].zName = sqlite3DbStrDup(pParse->db, pTab->aCol[pTab->iPKey].zName); pList->a[0].sortOrder = pParse->iPkSortOrder; assert( pParse->pNewTable==pTab ); pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0); if( pPk==0 ) return; pTab->iPKey = -1; }else{ pPk = sqlite3PrimaryKeyIndex(pTab); } assert( pPk!=0 ); nPk = pPk->nKeyCol; | > | 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 | if( pList==0 ) return; pList->a[0].zName = sqlite3DbStrDup(pParse->db, pTab->aCol[pTab->iPKey].zName); pList->a[0].sortOrder = pParse->iPkSortOrder; assert( pParse->pNewTable==pTab ); pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0); if( pPk==0 ) return; pPk->autoIndex = 2; pTab->iPKey = -1; }else{ pPk = sqlite3PrimaryKeyIndex(pTab); } assert( pPk!=0 ); nPk = pPk->nKeyCol; |
︙ | ︙ |
Changes to src/delete.c.
︙ | ︙ | |||
395 396 397 398 399 400 401 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey); sqlite3WhereEnd(pWInfo); /* Open cursors for all indices of the table. */ sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, &iDataCur, &iIdxCur); | < < | 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey); sqlite3WhereEnd(pWInfo); /* Open cursors for all indices of the table. */ sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, &iDataCur, &iIdxCur); /* Loop over the primary keys to be deleted. */ addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph); sqlite3VdbeAddOp2(v, OP_RowKey, iEph, iPk); /* Delete the row */ sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
︙ | ︙ |
Changes to src/fkey.c.
︙ | ︙ | |||
540 541 542 543 544 545 546 | pWhere = sqlite3ExprAnd(db, pWhere, pEq); } /* If the child table is the same as the parent table, and this scan ** is taking place as part of a DELETE operation (operation D.2), omit the ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE ** clause, where $rowid is the rowid of the row being deleted. */ | | | 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | pWhere = sqlite3ExprAnd(db, pWhere, pEq); } /* If the child table is the same as the parent table, and this scan ** is taking place as part of a DELETE operation (operation D.2), omit the ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE ** clause, where $rowid is the rowid of the row being deleted. */ if( pTab==pFKey->pFrom && nIncr>0 && HasRowid(pTab) /*FIXME*/ ){ Expr *pEq; /* Expression (pLeft = pRight) */ Expr *pLeft; /* Value from parent table row */ Expr *pRight; /* Column ref to child table */ pLeft = sqlite3Expr(db, TK_REGISTER, 0); pRight = sqlite3Expr(db, TK_COLUMN, 0); if( pLeft && pRight ){ pLeft->iTable = regData; |
︙ | ︙ |
Changes to src/vdbeblob.c.
︙ | ︙ | |||
174 175 176 177 178 179 180 181 182 183 184 185 186 187 | sqlite3BtreeEnterAll(db); pTab = sqlite3LocateTable(pParse, 0, zTable, zDb); if( pTab && IsVirtual(pTab) ){ pTab = 0; sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable); } #ifndef SQLITE_OMIT_VIEW if( pTab && pTab->pSelect ){ pTab = 0; sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable); } #endif if( !pTab ){ | > > > > | 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | sqlite3BtreeEnterAll(db); pTab = sqlite3LocateTable(pParse, 0, zTable, zDb); if( pTab && IsVirtual(pTab) ){ pTab = 0; sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable); } if( pTab && !HasRowid(pTab) ){ pTab = 0; sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable); } #ifndef SQLITE_OMIT_VIEW if( pTab && pTab->pSelect ){ pTab = 0; sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable); } #endif if( !pTab ){ |
︙ | ︙ |
Changes to test/without_rowid1.test.
︙ | ︙ | |||
87 88 89 90 91 92 93 94 95 96 97 | do_execsql_test without_rowid1-1.40 { VACUUM; SELECT *, '|' FROM t1 ORDER BY b, d; } {1250 phone flipper harvard | journal sherman ammonia 3.1415926 | journal sherman gamma 3.1415926 | arctic sleep ammonia helena |} integrity_check without_rowid1-1.41 do_execsql_test without_rowid1-1.50 { ANALYZE; SELECT * FROM sqlite_stat1 ORDER BY idx; } {t1 t1 {4 2 1} t1 t1bd {4 2 2}} | > > > > > > | > > > > > | 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 | do_execsql_test without_rowid1-1.40 { VACUUM; SELECT *, '|' FROM t1 ORDER BY b, d; } {1250 phone flipper harvard | journal sherman ammonia 3.1415926 | journal sherman gamma 3.1415926 | arctic sleep ammonia helena |} integrity_check without_rowid1-1.41 # Verify that ANALYZE works # do_execsql_test without_rowid1-1.50 { ANALYZE; SELECT * FROM sqlite_stat1 ORDER BY idx; } {t1 t1 {4 2 1} t1 t1bd {4 2 2}} ifcapable stat3 { do_execsql_test without_rowid1-1.51 { SELECT DISTINCT tbl, idx FROM sqlite_stat3 ORDER BY idx; } {t1 t1 t1 t1bd} } ifcapable stat4 { do_execsql_test without_rowid1-1.52 { SELECT DISTINCT tbl, idx FROM sqlite_stat4 ORDER BY idx; } {t1 t1 t1 t1bd} } finish_test |
Added test/without_rowid2.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 | # 2013-11-02 # # 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. The # focus of this file is testing WITHOUT ROWID tables, and especially # FOREIGN KEY constraints. # set testdir [file dirname $argv0] source $testdir/tester.tcl ifcapable {!foreignkey} { finish_test return } # Create a table and some data to work with. # do_test without_rowid2-1.0 { execsql { CREATE TABLE t1( a INT PRIMARY KEY, b INT REFERENCES t1 ON DELETE CASCADE REFERENCES t2, c TEXT, FOREIGN KEY (b,c) REFERENCES t2(x,y) ON UPDATE CASCADE ) WITHOUT rowid; } } {} do_test without_rowid2-1.1 { execsql { CREATE TABLE t2( x INT PRIMARY KEY, y TEXT ) WITHOUT rowid; } } {} do_test without_rowid2-1.2 { execsql { CREATE TABLE t3( a INT REFERENCES t2, b INT REFERENCES t1, FOREIGN KEY (a,b) REFERENCES t2(x,y) ); } } {} do_test without_rowid2-2.1 { execsql { CREATE TABLE t4(a int primary key) WITHOUT rowid; CREATE TABLE t5(x references t4); CREATE TABLE t6(x references t4); CREATE TABLE t7(x references t4); CREATE TABLE t8(x references t4); CREATE TABLE t9(x references t4); CREATE TABLE t10(x references t4); DROP TABLE t7; DROP TABLE t9; DROP TABLE t5; DROP TABLE t8; DROP TABLE t6; DROP TABLE t10; } } {} do_test without_rowid2-3.1 { execsql { CREATE TABLE t5(a PRIMARY KEY, b, c) WITHOUT rowid; CREATE TABLE t6( d REFERENCES t5, e REFERENCES t5(c) ); PRAGMA foreign_key_list(t6); } } [concat \ {0 0 t5 e c {NO ACTION} {NO ACTION} NONE} \ {1 0 t5 d {} {NO ACTION} {NO ACTION} NONE} \ ] do_test without_rowid2-3.2 { execsql { CREATE TABLE t7(d, e, f, FOREIGN KEY (d, e) REFERENCES t5(a, b) ); PRAGMA foreign_key_list(t7); } } [concat \ {0 0 t5 d a {NO ACTION} {NO ACTION} NONE} \ {0 1 t5 e b {NO ACTION} {NO ACTION} NONE} \ ] do_test without_rowid2-3.3 { execsql { CREATE TABLE t8(d, e, f, FOREIGN KEY (d, e) REFERENCES t5 ON DELETE CASCADE ON UPDATE SET NULL ); PRAGMA foreign_key_list(t8); } } [concat \ {0 0 t5 d {} {SET NULL} CASCADE NONE} \ {0 1 t5 e {} {SET NULL} CASCADE NONE} \ ] do_test without_rowid2-3.4 { execsql { CREATE TABLE t9(d, e, f, FOREIGN KEY (d, e) REFERENCES t5 ON DELETE CASCADE ON UPDATE SET DEFAULT ); PRAGMA foreign_key_list(t9); } } [concat \ {0 0 t5 d {} {SET DEFAULT} CASCADE NONE} \ {0 1 t5 e {} {SET DEFAULT} CASCADE NONE} \ ] do_test without_rowid2-3.5 { sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0 } {0 0 0} finish_test |
Added test/without_rowid3.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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 | # 2013-11-02 # # 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 on WITHOUT ROWID # tables. # set testdir [file dirname $argv0] source $testdir/tester.tcl ifcapable {!foreignkey||!trigger} { finish_test return } #------------------------------------------------------------------------- # Test structure: # # without_rowid3-1.*: Simple tests to check that immediate and deferred foreign key # constraints work when not inside a transaction. # # without_rowid3-2.*: Tests to verify that deferred foreign keys work inside # explicit transactions (i.e that processing really is deferred). # # without_rowid3-3.*: Tests that a statement transaction is rolled back if an # immediate foreign key constraint is violated. # # without_rowid3-4.*: Test that FK actions may recurse even when recursive triggers # are disabled. # # without_rowid3-5.*: Check that if foreign-keys are enabled, it is not possible # to write to an FK column using the incremental blob API. # # without_rowid3-6.*: Test that FK processing is automatically disabled when # running VACUUM. # # without_rowid3-7.*: Test using an IPK as the key in the child (referencing) table. # # without_rowid3-8.*: Test that enabling/disabling foreign key support while a # transaction is active is not possible. # # without_rowid3-9.*: Test SET DEFAULT actions. # # without_rowid3-10.*: Test errors. # # without_rowid3-11.*: Test CASCADE actions. # # without_rowid3-12.*: Test RESTRICT actions. # # without_rowid3-13.*: Test that FK processing is performed when a row is REPLACED by # an UPDATE or INSERT statement. # # without_rowid3-14.*: Test the ALTER TABLE and DROP TABLE commands. # # without_rowid3-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. # # without_rowid3-16.*: Test that rows that refer to themselves may be inserted, # updated and deleted. # # without_rowid3-17.*: Test that the "count_changes" pragma does not interfere with # FK constraint processing. # # without_rowid3-18.*: Test that the authorization callback is invoked when processing # FK constraints. # # without_rowid3-20.*: Test that ON CONFLICT clauses specified as part of statements # do not affect the operation of FK constraints. # # without_rowid3-genfkey.*: Tests that were used with the shell tool .genfkey # command. Recycled to test the built-in implementation. # # without_rowid3-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) WITHOUT rowid; CREATE TABLE t2(c REFERENCES t1(a) /D/ , d); CREATE TABLE t3(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE t4(c REFERENCES t3 /D/, d); CREATE TABLE t7(a, b INT PRIMARY KEY) WITHOUT rowid; 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.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 without_rowid3-1.1.0 { execsql [string map {/D/ {}} $FkeySimpleSchema] } {} foreach {tn zSql res} $FkeySimpleTests { do_test without_rowid3-1.1.$tn.1 { catchsql $zSql } $res do_test without_rowid3-1.1.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {} do_test without_rowid3-1.1.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {} do_test without_rowid3-1.1.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {} do_test without_rowid3-1.1.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {} do_test without_rowid3-1.1.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {} do_test without_rowid3-1.1.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {} } drop_all_tables do_test without_rowid3-1.2.0 { execsql [string map {/D/ {DEFERRABLE INITIALLY DEFERRED}} $FkeySimpleSchema] } {} foreach {tn zSql res} $FkeySimpleTests { do_test without_rowid3-1.2.$tn { catchsql $zSql } $res do_test without_rowid3-1.2.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {} do_test without_rowid3-1.2.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {} do_test without_rowid3-1.2.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {} do_test without_rowid3-1.2.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {} do_test without_rowid3-1.2.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {} do_test without_rowid3-1.2.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {} } drop_all_tables do_test without_rowid3-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 without_rowid3-1.3.$tn { catchsql $zSql } $res do_test without_rowid3-1.3.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {} do_test without_rowid3-1.3.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {} do_test without_rowid3-1.3.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {} do_test without_rowid3-1.3.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {} do_test without_rowid3-1.3.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {} do_test without_rowid3-1.3.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {} } execsql { PRAGMA count_changes = 0 } drop_all_tables do_test without_rowid3-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 without_rowid3-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 without_rowid3-1.5.1 { execsql { CREATE TABLE i(i INT PRIMARY KEY) WITHOUT rowid; 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 without_rowid3-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 without_rowid3-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 without_rowid3-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 without_rowid3-1.7.1 { execsql { CREATE TABLE i(i TEXT COLLATE nocase PRIMARY KEY) WITHOUT rowid; 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 without_rowid3-1.7.2 { execsql { CREATE TABLE i(i TEXT PRIMARY KEY) WITHOUT rowid; -- 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 without_rowid3-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 without_rowid3-2.*) contains tests to check that the # deferred foreign key constraint logic works. # proc without_rowid3-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 without_rowid3-2.$tn [list catchsql $sql] $expected if {$nocommit} { do_test without_rowid3-2.${tn}c { catchsql COMMIT } {1 {foreign key constraint failed}} } } without_rowid3-2-test 1 0 { CREATE TABLE node( nodeid PRIMARY KEY, parent REFERENCES node DEFERRABLE INITIALLY DEFERRED ) WITHOUT rowid; CREATE TABLE leaf( cellid PRIMARY KEY, parent REFERENCES node DEFERRABLE INITIALLY DEFERRED ) WITHOUT rowid; } without_rowid3-2-test 1 0 "INSERT INTO node VALUES(1, 0)" FKV without_rowid3-2-test 2 0 "BEGIN" without_rowid3-2-test 3 1 "INSERT INTO node VALUES(1, 0)" without_rowid3-2-test 4 0 "UPDATE node SET parent = NULL" without_rowid3-2-test 5 0 "COMMIT" without_rowid3-2-test 6 0 "SELECT * FROM node" {1 {}} without_rowid3-2-test 7 0 "BEGIN" without_rowid3-2-test 8 1 "INSERT INTO leaf VALUES('a', 2)" without_rowid3-2-test 9 1 "INSERT INTO node VALUES(2, 0)" without_rowid3-2-test 10 0 "UPDATE node SET parent = 1 WHERE nodeid = 2" without_rowid3-2-test 11 0 "COMMIT" without_rowid3-2-test 12 0 "SELECT * FROM node" {1 {} 2 1} without_rowid3-2-test 13 0 "SELECT * FROM leaf" {a 2} without_rowid3-2-test 14 0 "BEGIN" without_rowid3-2-test 15 1 "DELETE FROM node WHERE nodeid = 2" without_rowid3-2-test 16 0 "INSERT INTO node VALUES(2, NULL)" without_rowid3-2-test 17 0 "COMMIT" without_rowid3-2-test 18 0 "SELECT * FROM node" {1 {} 2 {}} without_rowid3-2-test 19 0 "SELECT * FROM leaf" {a 2} without_rowid3-2-test 20 0 "BEGIN" without_rowid3-2-test 21 0 "INSERT INTO leaf VALUES('b', 1)" without_rowid3-2-test 22 0 "SAVEPOINT save" without_rowid3-2-test 23 0 "DELETE FROM node WHERE nodeid = 1" without_rowid3-2-test 24 0 "ROLLBACK TO save" without_rowid3-2-test 25 0 "COMMIT" without_rowid3-2-test 26 0 "SELECT * FROM node" {1 {} 2 {}} without_rowid3-2-test 27 0 "SELECT * FROM leaf" {a 2 b 1} without_rowid3-2-test 28 0 "BEGIN" without_rowid3-2-test 29 0 "INSERT INTO leaf VALUES('c', 1)" without_rowid3-2-test 30 0 "SAVEPOINT save" without_rowid3-2-test 31 0 "DELETE FROM node WHERE nodeid = 1" without_rowid3-2-test 32 1 "RELEASE save" without_rowid3-2-test 33 1 "DELETE FROM leaf WHERE cellid = 'b'" without_rowid3-2-test 34 0 "DELETE FROM leaf WHERE cellid = 'c'" without_rowid3-2-test 35 0 "COMMIT" without_rowid3-2-test 36 0 "SELECT * FROM node" {2 {}} without_rowid3-2-test 37 0 "SELECT * FROM leaf" {a 2} without_rowid3-2-test 38 0 "SAVEPOINT outer" without_rowid3-2-test 39 1 "INSERT INTO leaf VALUES('d', 3)" without_rowid3-2-test 40 1 "RELEASE outer" FKV without_rowid3-2-test 41 1 "INSERT INTO leaf VALUES('e', 3)" without_rowid3-2-test 42 0 "INSERT INTO node VALUES(3, 2)" without_rowid3-2-test 43 0 "RELEASE outer" without_rowid3-2-test 44 0 "SAVEPOINT outer" without_rowid3-2-test 45 1 "DELETE FROM node WHERE nodeid=3" without_rowid3-2-test 47 0 "INSERT INTO node VALUES(3, 2)" without_rowid3-2-test 48 0 "ROLLBACK TO outer" without_rowid3-2-test 49 0 "RELEASE outer" without_rowid3-2-test 50 0 "SAVEPOINT outer" without_rowid3-2-test 51 1 "INSERT INTO leaf VALUES('f', 4)" without_rowid3-2-test 52 1 "SAVEPOINT inner" without_rowid3-2-test 53 1 "INSERT INTO leaf VALUES('g', 4)" without_rowid3-2-test 54 1 "RELEASE outer" FKV without_rowid3-2-test 55 1 "ROLLBACK TO inner" without_rowid3-2-test 56 0 "COMMIT" FKV without_rowid3-2-test 57 0 "INSERT INTO node VALUES(4, NULL)" without_rowid3-2-test 58 0 "RELEASE outer" without_rowid3-2-test 59 0 "SELECT * FROM node" {2 {} 3 2 4 {}} without_rowid3-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. # without_rowid3-2-test 61 0 "BEGIN" without_rowid3-2-test 62 0 "DELETE FROM leaf" without_rowid3-2-test 63 0 "DELETE FROM node" without_rowid3-2-test 64 1 "INSERT INTO leaf VALUES('a', 1)" without_rowid3-2-test 65 1 "INSERT INTO leaf VALUES('b', 2)" without_rowid3-2-test 66 1 "INSERT INTO leaf VALUES('c', 1)" do_test without_rowid3-2-test-67 { catchsql "INSERT INTO node SELECT parent, 3 FROM leaf" } {1 {column nodeid is not unique}} without_rowid3-2-test 68 0 "COMMIT" FKV without_rowid3-2-test 69 1 "INSERT INTO node VALUES(1, NULL)" without_rowid3-2-test 70 0 "INSERT INTO node VALUES(2, NULL)" without_rowid3-2-test 71 0 "COMMIT" without_rowid3-2-test 72 0 "BEGIN" without_rowid3-2-test 73 1 "DELETE FROM node" without_rowid3-2-test 74 0 "INSERT INTO node(nodeid) SELECT DISTINCT parent FROM leaf" without_rowid3-2-test 75 0 "COMMIT" #------------------------------------------------------------------------- # Test cases without_rowid3-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. # # without_rowid3-3.1.*: Test UPDATE statements. # without_rowid3-3.2.*: Test DELETE statements. # drop_all_tables do_test without_rowid3-3.1.1 { execsql { CREATE TABLE ab(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE cd( c PRIMARY KEY REFERENCES ab ON UPDATE CASCADE ON DELETE CASCADE, d ) WITHOUT rowid; CREATE TABLE ef( e REFERENCES cd ON UPDATE CASCADE, f, CHECK (e!=5) ); } } {} do_test without_rowid3-3.1.2 { execsql { INSERT INTO ab VALUES(1, 'b'); INSERT INTO cd VALUES(1, 'd'); INSERT INTO ef VALUES(1, 'e'); } } {} do_test without_rowid3-3.1.3 { catchsql { UPDATE ab SET a = 5 } } {1 {constraint failed}} do_test without_rowid3-3.1.4 { execsql { SELECT * FROM ab } } {1 b} do_test without_rowid3-3.1.4 { execsql BEGIN; catchsql { UPDATE ab SET a = 5 } } {1 {constraint failed}} do_test without_rowid3-3.1.5 { execsql COMMIT; execsql { SELECT * FROM ab; SELECT * FROM cd; SELECT * FROM ef } } {1 b 1 d 1 e} do_test without_rowid3-3.2.1 { execsql BEGIN; catchsql { DELETE FROM ab } } {1 {foreign key constraint failed}} do_test without_rowid3-3.2.2 { execsql COMMIT execsql { SELECT * FROM ab; SELECT * FROM cd; SELECT * FROM ef } } {1 b 1 d 1 e} #------------------------------------------------------------------------- # Test cases without_rowid3-4.* test that recursive foreign key actions # (i.e. CASCADE) are allowed even if recursive triggers are disabled. # drop_all_tables do_test without_rowid3-4.1 { execsql { CREATE TABLE t1( node PRIMARY KEY, parent REFERENCES t1 ON DELETE CASCADE ) WITHOUT rowid; CREATE TABLE t2(node PRIMARY KEY, parent) WITHOUT rowid; 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 without_rowid3-4.2 { execsql { PRAGMA recursive_triggers = off } execsql { BEGIN; DELETE FROM t1 WHERE node = 1; SELECT node FROM t1; } } {} do_test without_rowid3-4.3 { execsql { DELETE FROM t2 WHERE node = 1; SELECT node FROM t2; ROLLBACK; } } {4 5 6 7} do_test without_rowid3-4.4 { execsql { PRAGMA recursive_triggers = on } execsql { BEGIN; DELETE FROM t1 WHERE node = 1; SELECT node FROM t1; } } {} do_test without_rowid3-4.3 { execsql { DELETE FROM t2 WHERE node = 1; SELECT node FROM t2; ROLLBACK; } } {} #------------------------------------------------------------------------- # Test cases without_rowid3-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 without_rowid3-5.1 { execsql { CREATE TABLE t1(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1(a)) WITHOUT rowid; INSERT INTO t1 VALUES('hello', 'world'); INSERT INTO t2 VALUES('key', 'hello'); } } {} do_test without_rowid3-5.2 { set rc [catch { set fd [db incrblob t2 b 1] } msg] list $rc $msg } {1 {cannot open table without rowid: t2}} do_test without_rowid3-5.5 { execsql { PRAGMA foreign_keys = on } } {} } drop_all_tables ifcapable vacuum { do_test without_rowid3-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 INT PRIMARY KEY as the child key # of a foreign constraint. # drop_all_tables do_test without_rowid3-7.1 { execsql { CREATE TABLE t1(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE t2(c INT PRIMARY KEY REFERENCES t1, b) WITHOUT rowid; } } {} do_test without_rowid3-7.2 { catchsql { INSERT INTO t2 VALUES(1, 'A'); } } {1 {foreign key constraint failed}} do_test without_rowid3-7.3 { execsql { INSERT INTO t1 VALUES(1, 2); INSERT INTO t1 VALUES(2, 3); INSERT INTO t2 VALUES(1, 'A'); } } {} do_test without_rowid3-7.4 { execsql { UPDATE t2 SET c = 2 } } {} do_test without_rowid3-7.5 { catchsql { UPDATE t2 SET c = 3 } } {1 {foreign key constraint failed}} do_test without_rowid3-7.6 { catchsql { DELETE FROM t1 WHERE a = 2 } } {1 {foreign key constraint failed}} do_test without_rowid3-7.7 { execsql { DELETE FROM t1 WHERE a = 1 } } {} do_test without_rowid3-7.8 { catchsql { UPDATE t1 SET a = 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 without_rowid3-8-test {tn zSql value} { do_test without_rowid3-2.8.$tn.1 [list execsql $zSql] {} do_test without_rowid3-2.8.$tn.2 { execsql "PRAGMA foreign_keys" } $value } without_rowid3-8-test 1 { PRAGMA foreign_keys = 0 } 0 without_rowid3-8-test 2 { PRAGMA foreign_keys = 1 } 1 without_rowid3-8-test 3 { BEGIN } 1 without_rowid3-8-test 4 { PRAGMA foreign_keys = 0 } 1 without_rowid3-8-test 5 { COMMIT } 1 without_rowid3-8-test 6 { PRAGMA foreign_keys = 0 } 0 without_rowid3-8-test 7 { BEGIN } 0 without_rowid3-8-test 8 { PRAGMA foreign_keys = 1 } 0 without_rowid3-8-test 9 { COMMIT } 0 without_rowid3-8-test 10 { PRAGMA foreign_keys = 1 } 1 without_rowid3-8-test 11 { PRAGMA foreign_keys = off } 0 without_rowid3-8-test 12 { PRAGMA foreign_keys = on } 1 without_rowid3-8-test 13 { PRAGMA foreign_keys = no } 0 without_rowid3-8-test 14 { PRAGMA foreign_keys = yes } 1 without_rowid3-8-test 15 { PRAGMA foreign_keys = false } 0 without_rowid3-8-test 16 { PRAGMA foreign_keys = true } 1 #------------------------------------------------------------------------- # The following tests, without_rowid3-9.*, test SET DEFAULT actions. # drop_all_tables do_test without_rowid3-9.1.1 { execsql { CREATE TABLE t1(a INT PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE t2( c INT PRIMARY KEY, d INTEGER DEFAULT 1 REFERENCES t1 ON DELETE SET DEFAULT ) WITHOUT rowid; DELETE FROM t1; } } {} do_test without_rowid3-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 without_rowid3-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 without_rowid3-9.1.4 { execsql { SELECT * FROM t1 } } {2 two} do_test without_rowid3-9.1.5 { catchsql { DELETE FROM t1 } } {1 {foreign key constraint failed}} do_test without_rowid3-9.2.1 { execsql { CREATE TABLE pp(a, b, c, PRIMARY KEY(b, c)) WITHOUT rowid; 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 without_rowid3-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 without_rowid3-9.2.3 { execsql { DELETE FROM pp WHERE a = 4; SELECT * FROM cc; } } {{} A {} {} B {} 3 A 2 3 B 2} #------------------------------------------------------------------------- # The following tests, without_rowid3-10.*, test "foreign key mismatch" and # other errors. # set tn 0 foreach zSql [list { CREATE TABLE p(a PRIMARY KEY, b) WITHOUT rowid; 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)) WITHOUT rowid; 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 without_rowid3-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 without_rowid3-10.2.1 { drop_all_tables catchsql { CREATE TABLE t1(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE t2(c, d, FOREIGN KEY(rowid) REFERENCES t1(a)); } } {1 {unknown column "rowid" in foreign key definition}} do_test without_rowid3-10.2.2 { drop_all_tables catchsql { CREATE TABLE t1(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE t2(rowid, d, FOREIGN KEY(rowid) REFERENCES t1(a)); } } {0 {}} do_test without_rowid3-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 without_rowid3-10.2.2 { drop_all_tables catchsql { CREATE TABLE t1(rowid PRIMARY KEY, b) WITHOUT rowid; 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, without_rowid3-11.*, test CASCADE actions. # drop_all_tables do_test without_rowid3-11.1.1 { execsql { CREATE TABLE t1(a INT PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE t2(c, d, FOREIGN KEY(c) REFERENCES t1(a) ON UPDATE CASCADE); INSERT INTO t1 VALUES(10, 100); INSERT INTO t2 VALUES(10, 100); UPDATE t1 SET a = 15; SELECT * FROM t2; } } {15 100} #------------------------------------------------------------------------- # The following tests, without_rowid3-12.*, test RESTRICT actions. # drop_all_tables do_test without_rowid3-12.1.1 { execsql { CREATE TABLE t1(a, b PRIMARY KEY) WITHOUT rowid; 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 without_rowid3-12.1.2 { execsql "BEGIN" execsql "INSERT INTO t2 VALUES('two')" } {} do_test without_rowid3-12.1.3 { execsql "UPDATE t1 SET b = 'four' WHERE b = 'one'" } {} do_test without_rowid3-12.1.4 { catchsql "UPDATE t1 SET b = 'five' WHERE b = 'two'" } {1 {foreign key constraint failed}} do_test without_rowid3-12.1.5 { execsql "DELETE FROM t1 WHERE b = 'two'" } {} do_test without_rowid3-12.1.6 { catchsql "COMMIT" } {1 {foreign key constraint failed}} do_test without_rowid3-12.1.7 { execsql { INSERT INTO t1 VALUES(2, 'two'); COMMIT; } } {} drop_all_tables do_test without_rowid3-12.2.1 { execsql { CREATE TABLE t1(x COLLATE NOCASE PRIMARY KEY) WITHOUT rowid; 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 without_rowid3-12.2.2 { execsql { DELETE FROM t1 } execsql { SELECT * FROM t1; SELECT * FROM t2; } } {A B a b} do_test without_rowid3-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 without_rowid3-12.2.4 { execsql { SELECT * FROM t1; SELECT * FROM t2; } } {A B a b} drop_all_tables do_test without_rowid3-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) ) WITHOUT rowid; 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 without_rowid3-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 without_rowid3-12.3.3 { catchsql { INSERT INTO down(c39, c38) VALUES('yes', 'no') } } {1 {foreign key constraint failed}} do_test without_rowid3-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 without_rowid3-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, without_rowid3-13.*, test that FK processing is performed # when rows are REPLACEd. # drop_all_tables do_test without_rowid3-13.1.1 { execsql { CREATE TABLE pp(a UNIQUE, b, c, PRIMARY KEY(b, c)) WITHOUT rowid; 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)" } { do_test without_rowid3-13.1.$tn.1 { catchsql $stmt } {1 {foreign key constraint failed}} do_test without_rowid3-13.1.$tn.2 { execsql { SELECT * FROM pp; SELECT * FROM cc; } } {1 2 3 2 3 1} do_test without_rowid3-13.1.$tn.3 { execsql BEGIN; catchsql $stmt } {1 {foreign key constraint failed}} do_test without_rowid3-13.1.$tn.4 { execsql { COMMIT; SELECT * FROM pp; SELECT * FROM cc; } } {1 2 3 2 3 1} } #------------------------------------------------------------------------- # The following tests, without_rowid3-14.*, test that the "DROP TABLE" and "ALTER # TABLE" commands work as expected wrt foreign key constraints. # # without_rowid3-14.1*: ALTER TABLE ADD COLUMN # without_rowid3-14.2*: ALTER TABLE RENAME TABLE # without_rowid3-14.3*: DROP TABLE # drop_all_tables ifcapable altertable { do_test without_rowid3-14.1.1 { # Adding a column with a REFERENCES clause is not supported. execsql { CREATE TABLE t1(a PRIMARY KEY) WITHOUT rowid; CREATE TABLE t2(a, b); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} do_test without_rowid3-14.1.2 { catchsql { ALTER TABLE t2 ADD COLUMN d DEFAULT NULL REFERENCES t1 } } {0 {}} do_test without_rowid3-14.1.3 { catchsql { ALTER TABLE t2 ADD COLUMN e REFERENCES t1 DEFAULT NULL} } {0 {}} do_test without_rowid3-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 without_rowid3-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 without_rowid3-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_parent($zCreate, $zOld, $zNew)} } do_test without_rowid3-14.2.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} do_test without_rowid3-14.2.1.2 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3 } {{CREATE TABLE t1(a REFERENCES t2)}} do_test without_rowid3-14.2.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} # Test ALTER TABLE RENAME TABLE a bit. # do_test without_rowid3-14.2.2.1 { drop_all_tables execsql { CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid; CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) WITHOUT rowid; CREATE TABLE t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1); } execsql { SELECT sql FROM sqlite_master WHERE type = 'table'} } [list \ {CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) WITHOUT rowid} \ {CREATE TABLE t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1)} \ ] do_test without_rowid3-14.2.2.2 { execsql { ALTER TABLE t1 RENAME TO t4 } execsql { SELECT sql FROM sqlite_master WHERE type = 'table'} } [list \ {CREATE TABLE "t4"(a PRIMARY KEY, b REFERENCES "t4") WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES "t4", c REFERENCES t2) WITHOUT rowid} \ {CREATE TABLE t3(a REFERENCES "t4", b REFERENCES t2, c REFERENCES "t4")} \ ] do_test without_rowid3-14.2.2.3 { catchsql { INSERT INTO t3 VALUES(1, 2, 3) } } {1 {foreign key constraint failed}} do_test without_rowid3-14.2.2.4 { execsql { INSERT INTO t4 VALUES(1, NULL) } } {} do_test without_rowid3-14.2.2.5 { catchsql { UPDATE t4 SET b = 5 } } {1 {foreign key constraint failed}} do_test without_rowid3-14.2.2.6 { catchsql { UPDATE t4 SET b = 1 } } {0 {}} do_test without_rowid3-14.2.2.7 { execsql { INSERT INTO t3 VALUES(1, NULL, 1) } } {} # Repeat for TEMP tables # drop_all_tables do_test without_rowid3-14.1tmp.1 { # Adding a column with a REFERENCES clause is not supported. execsql { CREATE TEMP TABLE t1(a PRIMARY KEY) WITHOUT rowid; CREATE TEMP TABLE t2(a, b); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} do_test without_rowid3-14.1tmp.2 { catchsql { ALTER TABLE t2 ADD COLUMN d DEFAULT NULL REFERENCES t1 } } {0 {}} do_test without_rowid3-14.1tmp.3 { catchsql { ALTER TABLE t2 ADD COLUMN e REFERENCES t1 DEFAULT NULL} } {0 {}} do_test without_rowid3-14.1tmp.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 without_rowid3-14.1tmp.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 without_rowid3-14.1tmp.6 { execsql { PRAGMA foreign_keys = off; ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1; PRAGMA foreign_keys = on; SELECT sql FROM sqlite_temp_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)}} do_test without_rowid3-14.2tmp.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} do_test without_rowid3-14.2tmp.1.2 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3 } {{CREATE TABLE t1(a REFERENCES t2)}} do_test without_rowid3-14.2tmp.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} # Test ALTER TABLE RENAME TABLE a bit. # do_test without_rowid3-14.2tmp.2.1 { drop_all_tables execsql { CREATE TEMP TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid; CREATE TEMP TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) WITHOUT rowid; CREATE TEMP TABLE t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1); } execsql { SELECT sql FROM sqlite_temp_master WHERE type = 'table'} } [list \ {CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) WITHOUT rowid} \ {CREATE TABLE t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1)} \ ] do_test without_rowid3-14.2tmp.2.2 { execsql { ALTER TABLE t1 RENAME TO t4 } execsql { SELECT sql FROM sqlite_temp_master WHERE type = 'table'} } [list \ {CREATE TABLE "t4"(a PRIMARY KEY, b REFERENCES "t4") WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES "t4", c REFERENCES t2) WITHOUT rowid} \ {CREATE TABLE t3(a REFERENCES "t4", b REFERENCES t2, c REFERENCES "t4")} \ ] do_test without_rowid3-14.2tmp.2.3 { catchsql { INSERT INTO t3 VALUES(1, 2, 3) } } {1 {foreign key constraint failed}} do_test without_rowid3-14.2tmp.2.4 { execsql { INSERT INTO t4 VALUES(1, NULL) } } {} do_test without_rowid3-14.2tmp.2.5 { catchsql { UPDATE t4 SET b = 5 } } {1 {foreign key constraint failed}} do_test without_rowid3-14.2tmp.2.6 { catchsql { UPDATE t4 SET b = 1 } } {0 {}} do_test without_rowid3-14.2tmp.2.7 { execsql { INSERT INTO t3 VALUES(1, NULL, 1) } } {} # Repeat for ATTACH-ed tables # drop_all_tables do_test without_rowid3-14.1aux.1 { # Adding a column with a REFERENCES clause is not supported. execsql { ATTACH ':memory:' AS aux; CREATE TABLE aux.t1(a PRIMARY KEY) WITHOUT rowid; CREATE TABLE aux.t2(a, b); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} do_test without_rowid3-14.1aux.2 { catchsql { ALTER TABLE t2 ADD COLUMN d DEFAULT NULL REFERENCES t1 } } {0 {}} do_test without_rowid3-14.1aux.3 { catchsql { ALTER TABLE t2 ADD COLUMN e REFERENCES t1 DEFAULT NULL} } {0 {}} do_test without_rowid3-14.1aux.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 without_rowid3-14.1aux.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 without_rowid3-14.1aux.6 { execsql { PRAGMA foreign_keys = off; ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1; PRAGMA foreign_keys = on; SELECT sql FROM aux.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)}} do_test without_rowid3-14.2aux.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} do_test without_rowid3-14.2aux.1.2 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3 } {{CREATE TABLE t1(a REFERENCES t2)}} do_test without_rowid3-14.2aux.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} # Test ALTER TABLE RENAME TABLE a bit. # do_test without_rowid3-14.2aux.2.1 { drop_all_tables execsql { CREATE TABLE aux.t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid; CREATE TABLE aux.t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) WITHOUT rowid; CREATE TABLE aux.t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1); } execsql { SELECT sql FROM aux.sqlite_master WHERE type = 'table'} } [list \ {CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) WITHOUT rowid} \ {CREATE TABLE t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1)} \ ] do_test without_rowid3-14.2aux.2.2 { execsql { ALTER TABLE t1 RENAME TO t4 } execsql { SELECT sql FROM aux.sqlite_master WHERE type = 'table'} } [list \ {CREATE TABLE "t4"(a PRIMARY KEY, b REFERENCES "t4") WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES "t4", c REFERENCES t2) WITHOUT rowid} \ {CREATE TABLE t3(a REFERENCES "t4", b REFERENCES t2, c REFERENCES "t4")} \ ] do_test without_rowid3-14.2aux.2.3 { catchsql { INSERT INTO t3 VALUES(1, 2, 3) } } {1 {foreign key constraint failed}} do_test without_rowid3-14.2aux.2.4 { execsql { INSERT INTO t4 VALUES(1, NULL) } } {} do_test without_rowid3-14.2aux.2.5 { catchsql { UPDATE t4 SET b = 5 } } {1 {foreign key constraint failed}} do_test without_rowid3-14.2aux.2.6 { catchsql { UPDATE t4 SET b = 1 } } {0 {}} do_test without_rowid3-14.2aux.2.7 { execsql { INSERT INTO t3 VALUES(1, NULL, 1) } } {} } do_test without_rowid3-2.14.3.1 { drop_all_tables execsql { CREATE TABLE t1(a, b REFERENCES nosuchtable); DROP TABLE t1; } } {} do_test without_rowid3-2.14.3.2 { execsql { CREATE TABLE t1(a PRIMARY KEY, b) WITHOUT rowid; INSERT INTO t1 VALUES('a', 1); CREATE TABLE t2(x REFERENCES t1); INSERT INTO t2 VALUES('a'); } } {} do_test without_rowid3-2.14.3.3 { catchsql { DROP TABLE t1 } } {1 {foreign key constraint failed}} do_test without_rowid3-2.14.3.4 { execsql { DELETE FROM t2; DROP TABLE t1; } } {} do_test without_rowid3-2.14.3.4 { catchsql { INSERT INTO t2 VALUES('x') } } {1 {no such table: main.t1}} do_test without_rowid3-2.14.3.5 { execsql { CREATE TABLE t1(x PRIMARY KEY) WITHOUT rowid; INSERT INTO t1 VALUES('x'); } execsql { INSERT INTO t2 VALUES('x') } } {} do_test without_rowid3-2.14.3.6 { catchsql { DROP TABLE t1 } } {1 {foreign key constraint failed}} do_test without_rowid3-2.14.3.7 { execsql { DROP TABLE t2; DROP TABLE t1; } } {} do_test without_rowid3-2.14.3.8 { execsql { CREATE TABLE pp(x, y, PRIMARY KEY(x, y)) WITHOUT ROWID; CREATE TABLE cc(a, b, FOREIGN KEY(a, b) REFERENCES pp(x, z)); } catchsql { INSERT INTO cc VALUES(1, 2) } } {1 {foreign key mismatch - "cc" referencing "pp"}} do_test without_rowid3-2.14.3.9 { execsql { DROP TABLE cc } } {} do_test without_rowid3-2.14.3.10 { execsql { CREATE TABLE cc(a, b, FOREIGN KEY(a, b) REFERENCES pp DEFERRABLE INITIALLY DEFERRED ); } execsql { INSERT INTO pp VALUES('a', 'b'); INSERT INTO cc VALUES('a', 'b'); BEGIN; DROP TABLE pp; CREATE TABLE pp(a, b, c, PRIMARY KEY(b, c)) WITHOUT rowid; INSERT INTO pp VALUES(1, 'a', 'b'); COMMIT; } } {} do_test without_rowid3-2.14.3.11 { execsql { BEGIN; DROP TABLE cc; DROP TABLE pp; COMMIT; } } {} do_test without_rowid3-2.14.3.12 { execsql { CREATE TABLE b1(a, b); CREATE TABLE b2(a, b REFERENCES b1); DROP TABLE b1; } } {} do_test without_rowid3-2.14.3.13 { execsql { CREATE TABLE b3(a, b REFERENCES b2 DEFERRABLE INITIALLY DEFERRED); DROP TABLE b2; } } {} # Test that nothing goes wrong when dropping a table that refers to a view. # Or dropping a view that an existing FK (incorrectly) refers to. Or either # of the above scenarios with a virtual table. drop_all_tables do_test without_rowid3-2.14.4.1 { execsql { CREATE TABLE t1(x REFERENCES v); CREATE VIEW v AS SELECT * FROM t1; } } {} do_test without_rowid3-2.14.4.2 { execsql { DROP VIEW v; } } {} ifcapable vtab { register_echo_module db do_test without_rowid3-2.14.4.3 { execsql { CREATE VIRTUAL TABLE v USING echo(t1) } } {} do_test without_rowid3-2.14.4.2 { execsql { DROP TABLE v; } } {} } #------------------------------------------------------------------------- # The following tests, without_rowid3-15.*, test that unnecessary FK related scans # and lookups are avoided when the constraint counters are zero. # drop_all_tables proc execsqlS {zSql} { set ::sqlite_search_count 0 set ::sqlite_found_count 0 set res [uplevel [list execsql $zSql]] concat [expr $::sqlite_found_count + $::sqlite_search_count] $res } do_test without_rowid3-15.1.1 { execsql { CREATE TABLE pp(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE cc(x, y REFERENCES pp DEFERRABLE INITIALLY DEFERRED); INSERT INTO pp VALUES(1, 'one'); INSERT INTO pp VALUES(2, 'two'); INSERT INTO cc VALUES('neung', 1); INSERT INTO cc VALUES('song', 2); } } {} do_test without_rowid3-15.1.2 { execsqlS { INSERT INTO pp VALUES(3, 'three') } } {0} do_test without_rowid3-15.1.3 { execsql { BEGIN; INSERT INTO cc VALUES('see', 4); -- Violates deferred constraint } execsqlS { INSERT INTO pp VALUES(5, 'five') } } {2} do_test without_rowid3-15.1.4 { execsql { DELETE FROM cc WHERE x = 'see' } execsqlS { INSERT INTO pp VALUES(6, 'six') } } {0} do_test without_rowid3-15.1.5 { execsql COMMIT } {} do_test without_rowid3-15.1.6 { execsql BEGIN execsqlS { DELETE FROM cc WHERE x = 'neung'; ROLLBACK; } } {1} do_test without_rowid3-15.1.7 { execsql { BEGIN; DELETE FROM pp WHERE a = 2; } execsqlS { DELETE FROM cc WHERE x = 'neung'; ROLLBACK; } } {2} #------------------------------------------------------------------------- # This next block of tests, without_rowid3-16.*, test that rows that refer to # themselves may be inserted and deleted. # foreach {tn zSchema} { 1 { CREATE TABLE self(a INTEGER PRIMARY KEY, b REFERENCES self(a)) WITHOUT rowid } 2 { CREATE TABLE self(a PRIMARY KEY, b REFERENCES self(a)) WITHOUT rowid } 3 { CREATE TABLE self(a UNIQUE, b INT PRIMARY KEY REFERENCES self(a)) WITHOUT rowid } } { drop_all_tables do_test without_rowid3-16.1.$tn.1 { execsql $zSchema execsql { INSERT INTO self VALUES(13, 13) } } {} do_test without_rowid3-16.1.$tn.2 { execsql { UPDATE self SET a = 14, b = 14 } } {} do_test without_rowid3-16.1.$tn.3 { catchsql { UPDATE self SET b = 15 } } {1 {foreign key constraint failed}} do_test without_rowid3-16.1.$tn.4 { catchsql { UPDATE self SET a = 15 } } {1 {foreign key constraint failed}} do_test without_rowid3-16.1.$tn.5 { catchsql { UPDATE self SET a = 15, b = 16 } } {1 {foreign key constraint failed}} do_test without_rowid3-16.1.$tn.6 { catchsql { UPDATE self SET a = 17, b = 17 } } {0 {}} do_test without_rowid3-16.1.$tn.7 { execsql { DELETE FROM self } } {} do_test without_rowid3-16.1.$tn.8 { catchsql { INSERT INTO self VALUES(20, 21) } } {1 {foreign key constraint failed}} } #------------------------------------------------------------------------- # This next block of tests, without_rowid3-17.*, tests that if "PRAGMA count_changes" # is turned on statements that violate immediate FK constraints return # SQLITE_CONSTRAINT immediately, not after returning a number of rows. # Whereas statements that violate deferred FK constraints return the number # of rows before failing. # # Also test that rows modified by FK actions are not counted in either the # returned row count or the values returned by sqlite3_changes(). Like # trigger related changes, they are included in sqlite3_total_changes() though. # drop_all_tables do_test without_rowid3-17.1.1 { execsql { PRAGMA count_changes = 1 } execsql { CREATE TABLE one(a, b, c, UNIQUE(b, c)); CREATE TABLE two(d, e, f, FOREIGN KEY(e, f) REFERENCES one(b, c)); INSERT INTO one VALUES(1, 2, 3); } } {1} do_test without_rowid3-17.1.2 { set STMT [sqlite3_prepare_v2 db "INSERT INTO two VALUES(4, 5, 6)" -1 dummy] sqlite3_step $STMT } {SQLITE_CONSTRAINT} verify_ex_errcode without_rowid3-17.1.2b SQLITE_CONSTRAINT_FOREIGNKEY ifcapable autoreset { do_test without_rowid3-17.1.3 { sqlite3_step $STMT } {SQLITE_CONSTRAINT} verify_ex_errcode without_rowid3-17.1.3b SQLITE_CONSTRAINT_FOREIGNKEY } else { do_test without_rowid3-17.1.3 { sqlite3_step $STMT } {SQLITE_MISUSE} } do_test without_rowid3-17.1.4 { sqlite3_finalize $STMT } {SQLITE_CONSTRAINT} verify_ex_errcode without_rowid3-17.1.4b SQLITE_CONSTRAINT_FOREIGNKEY do_test without_rowid3-17.1.5 { execsql { INSERT INTO one VALUES(2, 3, 4); INSERT INTO one VALUES(3, 4, 5); INSERT INTO two VALUES(1, 2, 3); INSERT INTO two VALUES(2, 3, 4); INSERT INTO two VALUES(3, 4, 5); } } {1 1 1 1 1} do_test without_rowid3-17.1.6 { catchsql { BEGIN; INSERT INTO one VALUES(0, 0, 0); UPDATE two SET e=e+1, f=f+1; } } {1 {foreign key constraint failed}} do_test without_rowid3-17.1.7 { execsql { SELECT * FROM one } } {1 2 3 2 3 4 3 4 5 0 0 0} do_test without_rowid3-17.1.8 { execsql { SELECT * FROM two } } {1 2 3 2 3 4 3 4 5} do_test without_rowid3-17.1.9 { execsql COMMIT } {} do_test without_rowid3-17.1.10 { execsql { CREATE TABLE three( g, h, i, FOREIGN KEY(h, i) REFERENCES one(b, c) DEFERRABLE INITIALLY DEFERRED ); } } {} do_test without_rowid3-17.1.11 { set STMT [sqlite3_prepare_v2 db "INSERT INTO three VALUES(7, 8, 9)" -1 dummy] sqlite3_step $STMT } {SQLITE_ROW} do_test without_rowid3-17.1.12 { sqlite3_column_text $STMT 0 } {1} do_test without_rowid3-17.1.13 { sqlite3_step $STMT } {SQLITE_CONSTRAINT} verify_ex_errcode without_rowid3-17.1.13b SQLITE_CONSTRAINT_FOREIGNKEY do_test without_rowid3-17.1.14 { sqlite3_finalize $STMT } {SQLITE_CONSTRAINT} verify_ex_errcode without_rowid3-17.1.14b SQLITE_CONSTRAINT_FOREIGNKEY drop_all_tables do_test without_rowid3-17.2.1 { execsql { CREATE TABLE high("a'b!" PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE low( c, "d&6" REFERENCES high ON UPDATE CASCADE ON DELETE CASCADE ); } } {} do_test without_rowid3-17.2.2 { execsql { INSERT INTO high VALUES('a', 'b'); INSERT INTO low VALUES('b', 'a'); } db changes } {1} set nTotal [db total_changes] do_test without_rowid3-17.2.3 { execsql { UPDATE high SET "a'b!" = 'c' } } {1} do_test without_rowid3-17.2.4 { db changes } {1} do_test without_rowid3-17.2.5 { expr [db total_changes] - $nTotal } {2} do_test without_rowid3-17.2.6 { execsql { SELECT * FROM high ; SELECT * FROM low } } {c b b c} do_test without_rowid3-17.2.7 { execsql { DELETE FROM high } } {1} do_test without_rowid3-17.2.8 { db changes } {1} do_test without_rowid3-17.2.9 { expr [db total_changes] - $nTotal } {4} do_test without_rowid3-17.2.10 { execsql { SELECT * FROM high ; SELECT * FROM low } } {} execsql { PRAGMA count_changes = 0 } #------------------------------------------------------------------------- # Test that the authorization callback works. # ifcapable auth { do_test without_rowid3-18.1 { execsql { CREATE TABLE long(a, b PRIMARY KEY, c) WITHOUT rowid; CREATE TABLE short(d, e, f REFERENCES long); CREATE TABLE mid(g, h, i REFERENCES long DEFERRABLE INITIALLY DEFERRED); } } {} proc auth {args} {eval lappend ::authargs $args ; return SQLITE_OK} db auth auth # An insert on the parent table must read the child key of any deferred # foreign key constraints. But not the child key of immediate constraints. set authargs {} do_test without_rowid3-18.2 { execsql { INSERT INTO long VALUES(1, 2, 3) } set authargs } {SQLITE_INSERT long {} main {} SQLITE_READ mid i main {}} # An insert on the child table of an immediate constraint must read the # parent key columns (to see if it is a violation or not). set authargs {} do_test without_rowid3-18.3 { execsql { INSERT INTO short VALUES(1, 3, 2) } set authargs } {SQLITE_INSERT short {} main {} SQLITE_READ long b main {}} # As must an insert on the child table of a deferred constraint. set authargs {} do_test without_rowid3-18.4 { execsql { INSERT INTO mid VALUES(1, 3, 2) } set authargs } {SQLITE_INSERT mid {} main {} SQLITE_READ long b main {}} do_test without_rowid3-18.5 { execsql { CREATE TABLE nought(a, b PRIMARY KEY, c) WITHOUT rowid; CREATE TABLE cross(d, e, f, FOREIGN KEY(e) REFERENCES nought(b) ON UPDATE CASCADE ); } execsql { INSERT INTO nought VALUES(2, 1, 2) } execsql { INSERT INTO cross VALUES(0, 1, 0) } set authargs [list] execsql { UPDATE nought SET b = 5 } set authargs } {SQLITE_UPDATE nought b main {} SQLITE_READ cross e main {} SQLITE_READ cross e main {} SQLITE_READ nought b main {} SQLITE_READ nought b main {} SQLITE_READ nought b main {} SQLITE_UPDATE cross e main {} SQLITE_READ nought b main {} SQLITE_READ cross e main {} SQLITE_READ nought b main {} SQLITE_READ nought b main {}} do_test without_rowid3-18.6 { execsql {SELECT * FROM cross} } {0 5 0} do_test without_rowid3-18.7 { execsql { CREATE TABLE one(a INT PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE two(b, c REFERENCES one); INSERT INTO one VALUES(101, 102); } set authargs [list] execsql { INSERT INTO two VALUES(100, 101); } set authargs } {SQLITE_INSERT two {} main {} SQLITE_READ one a main {}} # Return SQLITE_IGNORE to requests to read from the parent table. This # causes inserts of non-NULL keys into the child table to fail. # rename auth {} proc auth {args} { if {[lindex $args 1] == "long"} {return SQLITE_IGNORE} return SQLITE_OK } do_test without_rowid3-18.8 { catchsql { INSERT INTO short VALUES(1, 3, 2) } } {1 {foreign key constraint failed}} do_test without_rowid3-18.9 { execsql { INSERT INTO short VALUES(1, 3, NULL) } } {} do_test without_rowid3-18.10 { execsql { SELECT * FROM short } } {1 3 2 1 3 {}} do_test without_rowid3-18.11 { catchsql { UPDATE short SET f = 2 WHERE f IS NULL } } {1 {foreign key constraint failed}} db auth {} unset authargs } do_test without_rowid3-19.1 { execsql { CREATE TABLE main(id INT PRIMARY KEY) WITHOUT rowid; CREATE TABLE sub(id INT REFERENCES main(id)); INSERT INTO main VALUES(1); INSERT INTO main VALUES(2); INSERT INTO sub VALUES(2); } } {} do_test without_rowid3-19.2 { set S [sqlite3_prepare_v2 db "DELETE FROM main WHERE id = ?" -1 dummy] sqlite3_bind_int $S 1 2 sqlite3_step $S } {SQLITE_CONSTRAINT} verify_ex_errcode without_rowid3-19.2b SQLITE_CONSTRAINT_FOREIGNKEY do_test without_rowid3-19.3 { sqlite3_reset $S } {SQLITE_CONSTRAINT} verify_ex_errcode without_rowid3-19.3b SQLITE_CONSTRAINT_FOREIGNKEY do_test without_rowid3-19.4 { sqlite3_bind_int $S 1 1 sqlite3_step $S } {SQLITE_DONE} do_test without_rowid3-19.4 { sqlite3_finalize $S } {SQLITE_OK} drop_all_tables do_test without_rowid3-20.1 { execsql { CREATE TABLE pp(a PRIMARY KEY, b) WITHOUT rowid; CREATE TABLE cc(c PRIMARY KEY, d REFERENCES pp) WITHOUT rowid; } } {} foreach {tn insert} { 1 "INSERT" 2 "INSERT OR IGNORE" 3 "INSERT OR ABORT" 4 "INSERT OR ROLLBACK" 5 "INSERT OR REPLACE" 6 "INSERT OR FAIL" } { do_test without_rowid3-20.2.$tn.1 { catchsql "$insert INTO cc VALUES(1, 2)" } {1 {foreign key constraint failed}} do_test without_rowid3-20.2.$tn.2 { execsql { SELECT * FROM cc } } {} do_test without_rowid3-20.2.$tn.3 { execsql { BEGIN; INSERT INTO pp VALUES(2, 'two'); INSERT INTO cc VALUES(1, 2); } catchsql "$insert INTO cc VALUES(3, 4)" } {1 {foreign key constraint failed}} do_test without_rowid3-20.2.$tn.4 { execsql { COMMIT ; SELECT * FROM cc } } {1 2} do_test without_rowid3-20.2.$tn.5 { execsql { DELETE FROM cc ; DELETE FROM pp } } {} } foreach {tn update} { 1 "UPDATE" 2 "UPDATE OR IGNORE" 3 "UPDATE OR ABORT" 4 "UPDATE OR ROLLBACK" 5 "UPDATE OR REPLACE" 6 "UPDATE OR FAIL" } { do_test without_rowid3-20.3.$tn.1 { execsql { INSERT INTO pp VALUES(2, 'two'); INSERT INTO cc VALUES(1, 2); } } {} do_test without_rowid3-20.3.$tn.2 { catchsql "$update pp SET a = 1" } {1 {foreign key constraint failed}} do_test without_rowid3-20.3.$tn.3 { execsql { SELECT * FROM pp } } {2 two} do_test without_rowid3-20.3.$tn.4 { catchsql "$update cc SET d = 1" } {1 {foreign key constraint failed}} do_test without_rowid3-20.3.$tn.5 { execsql { SELECT * FROM cc } } {1 2} do_test without_rowid3-20.3.$tn.6 { execsql { BEGIN; INSERT INTO pp VALUES(3, 'three'); } catchsql "$update pp SET a = 1 WHERE a = 2" } {1 {foreign key constraint failed}} do_test without_rowid3-20.3.$tn.7 { execsql { COMMIT ; SELECT * FROM pp } } {2 two 3 three} do_test without_rowid3-20.3.$tn.8 { execsql { BEGIN; INSERT INTO cc VALUES(2, 2); } catchsql "$update cc SET d = 1 WHERE c = 1" } {1 {foreign key constraint failed}} do_test without_rowid3-20.3.$tn.9 { execsql { COMMIT ; SELECT * FROM cc } } {1 2 2 2} do_test without_rowid3-20.3.$tn.10 { execsql { DELETE FROM cc ; DELETE FROM pp } } {} } #------------------------------------------------------------------------- # The following block of tests, those prefixed with "without_rowid3-genfkey.", # are the same tests that were used to test the ".genfkey" command provided # by the shell tool. So these tests show that the built-in foreign key # implementation is more or less compatible with the triggers generated # by genfkey. # drop_all_tables do_test without_rowid3-genfkey.1.1 { execsql { CREATE TABLE t1(a INT PRIMARY KEY, b, c, UNIQUE(b, c)) WITHOUT rowid; CREATE TABLE t2(e REFERENCES t1, f); CREATE TABLE t3(g, h, i, FOREIGN KEY (h, i) REFERENCES t1(b, c)); } } {} do_test without_rowid3-genfkey.1.2 { catchsql { INSERT INTO t2 VALUES(1, 2) } } {1 {foreign key constraint failed}} do_test without_rowid3-genfkey.1.3 { execsql { INSERT INTO t1 VALUES(1, 2, 3); INSERT INTO t2 VALUES(1, 2); } } {} do_test without_rowid3-genfkey.1.4 { execsql { INSERT INTO t2 VALUES(NULL, 3) } } {} do_test without_rowid3-genfkey.1.5 { catchsql { UPDATE t2 SET e = 5 WHERE e IS NULL } } {1 {foreign key constraint failed}} do_test without_rowid3-genfkey.1.6 { execsql { UPDATE t2 SET e = 1 WHERE e IS NULL } } {} do_test without_rowid3-genfkey.1.7 { execsql { UPDATE t2 SET e = NULL WHERE f = 3 } } {} do_test without_rowid3-genfkey.1.8 { catchsql { UPDATE t1 SET a = 10 } } {1 {foreign key constraint failed}} do_test without_rowid3-genfkey.1.9 { catchsql { UPDATE t1 SET a = NULL } } {1 {t1.a may not be NULL}} do_test without_rowid3-genfkey.1.10 { catchsql { DELETE FROM t1 } } {1 {foreign key constraint failed}} do_test without_rowid3-genfkey.1.11 { execsql { UPDATE t2 SET e = NULL } } {} do_test without_rowid3-genfkey.1.12 { execsql { UPDATE t1 SET a = 10; DELETE FROM t1; DELETE FROM t2; } } {} do_test without_rowid3-genfkey.1.13 { execsql { INSERT INTO t3 VALUES(1, NULL, NULL); INSERT INTO t3 VALUES(1, 2, NULL); INSERT INTO t3 VALUES(1, NULL, 3); } } {} do_test without_rowid3-genfkey.1.14 { catchsql { INSERT INTO t3 VALUES(3, 1, 4) } } {1 {foreign key constraint failed}} do_test without_rowid3-genfkey.1.15 { execsql { INSERT INTO t1 VALUES(1, 1, 4); INSERT INTO t3 VALUES(3, 1, 4); } } {} do_test without_rowid3-genfkey.1.16 { catchsql { DELETE FROM t1 } } {1 {foreign key constraint failed}} do_test without_rowid3-genfkey.1.17 { catchsql { UPDATE t1 SET b = 10} } {1 {foreign key constraint failed}} do_test without_rowid3-genfkey.1.18 { execsql { UPDATE t1 SET a = 10} } {} do_test without_rowid3-genfkey.1.19 { catchsql { UPDATE t3 SET h = 'hello' WHERE i = 3} } {1 {foreign key constraint failed}} drop_all_tables do_test without_rowid3-genfkey.2.1 { execsql { CREATE TABLE t1(a INT PRIMARY KEY, b, c, UNIQUE(b, c)) WITHOUT rowid; CREATE TABLE t2(e REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE, f); CREATE TABLE t3(g, h, i, FOREIGN KEY (h, i) REFERENCES t1(b, c) ON UPDATE CASCADE ON DELETE CASCADE ); } } {} do_test without_rowid3-genfkey.2.2 { execsql { INSERT INTO t1 VALUES(1, 2, 3); INSERT INTO t1 VALUES(4, 5, 6); INSERT INTO t2 VALUES(1, 'one'); INSERT INTO t2 VALUES(4, 'four'); } } {} do_test without_rowid3-genfkey.2.3 { execsql { UPDATE t1 SET a = 2 WHERE a = 1; SELECT * FROM t2; } } {2 one 4 four} do_test without_rowid3-genfkey.2.4 { execsql { DELETE FROM t1 WHERE a = 4; SELECT * FROM t2; } } {2 one} do_test without_rowid3-genfkey.2.5 { execsql { INSERT INTO t3 VALUES('hello', 2, 3); UPDATE t1 SET c = 2; SELECT * FROM t3; } } {hello 2 2} do_test without_rowid3-genfkey.2.6 { execsql { DELETE FROM t1; SELECT * FROM t3; } } {} drop_all_tables do_test without_rowid3-genfkey.3.1 { execsql { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(c, b)) WITHOUT rowid; CREATE TABLE t2(e REFERENCES t1 ON UPDATE SET NULL ON DELETE SET NULL, f); CREATE TABLE t3(g, h, i, FOREIGN KEY (h, i) REFERENCES t1(b, c) ON UPDATE SET NULL ON DELETE SET NULL ); } } {} do_test without_rowid3-genfkey.3.2 { execsql { INSERT INTO t1 VALUES(1, 2, 3); INSERT INTO t1 VALUES(4, 5, 6); INSERT INTO t2 VALUES(1, 'one'); INSERT INTO t2 VALUES(4, 'four'); } } {} do_test without_rowid3-genfkey.3.3 { execsql { UPDATE t1 SET a = 2 WHERE a = 1; SELECT * FROM t2; } } {{} one 4 four} do_test without_rowid3-genfkey.3.4 { execsql { DELETE FROM t1 WHERE a = 4; SELECT * FROM t2; } } {{} one {} four} do_test without_rowid3-genfkey.3.5 { execsql { INSERT INTO t3 VALUES('hello', 2, 3); UPDATE t1 SET c = 2; SELECT * FROM t3; } } {hello {} {}} do_test without_rowid3-genfkey.3.6 { execsql { UPDATE t3 SET h = 2, i = 2; DELETE FROM t1; SELECT * FROM t3; } } {hello {} {}} #------------------------------------------------------------------------- # Verify that ticket dd08e5a988d00decc4a543daa8dbbfab9c577ad8 has been # fixed. # do_test without_rowid3-dd08e5.1.1 { execsql { PRAGMA foreign_keys=ON; CREATE TABLE tdd08(a INTEGER PRIMARY KEY, b) WITHOUT rowid; CREATE UNIQUE INDEX idd08 ON tdd08(a,b); INSERT INTO tdd08 VALUES(200,300); CREATE TABLE tdd08_b(w,x,y, FOREIGN KEY(x,y) REFERENCES tdd08(a,b)); INSERT INTO tdd08_b VALUES(100,200,300); } } {} do_test without_rowid3-dd08e5.1.2 { catchsql { DELETE FROM tdd08; } } {1 {foreign key constraint failed}} do_test without_rowid3-dd08e5.1.3 { execsql { SELECT * FROM tdd08; } } {200 300} do_test without_rowid3-dd08e5.1.4 { catchsql { INSERT INTO tdd08_b VALUES(400,500,300); } } {1 {foreign key constraint failed}} do_test without_rowid3-dd08e5.1.5 { catchsql { UPDATE tdd08_b SET x=x+1; } } {1 {foreign key constraint failed}} do_test without_rowid3-dd08e5.1.6 { catchsql { UPDATE tdd08 SET a=a+1; } } {1 {foreign key constraint failed}} #------------------------------------------------------------------------- # Verify that ticket ce7c133ea6cc9ccdc1a60d80441f80b6180f5eba # fixed. # do_test without_rowid3-ce7c13.1.1 { execsql { CREATE TABLE tce71(a INTEGER PRIMARY KEY, b) WITHOUT rowid; CREATE UNIQUE INDEX ice71 ON tce71(a,b); INSERT INTO tce71 VALUES(100,200); CREATE TABLE tce72(w, x, y, FOREIGN KEY(x,y) REFERENCES tce71(a,b)); INSERT INTO tce72 VALUES(300,100,200); UPDATE tce71 set b = 200 where a = 100; SELECT * FROM tce71, tce72; } } {100 200 300 100 200} do_test without_rowid3-ce7c13.1.2 { catchsql { UPDATE tce71 set b = 201 where a = 100; } } {1 {foreign key constraint failed}} do_test without_rowid3-ce7c13.1.3 { catchsql { UPDATE tce71 set a = 101 where a = 100; } } {1 {foreign key constraint failed}} do_test without_rowid3-ce7c13.1.4 { execsql { CREATE TABLE tce73(a INTEGER PRIMARY KEY, b, UNIQUE(a,b)) WITHOUT rowid; INSERT INTO tce73 VALUES(100,200); CREATE TABLE tce74(w, x, y, FOREIGN KEY(x,y) REFERENCES tce73(a,b)); INSERT INTO tce74 VALUES(300,100,200); UPDATE tce73 set b = 200 where a = 100; SELECT * FROM tce73, tce74; } } {100 200 300 100 200} do_test without_rowid3-ce7c13.1.5 { catchsql { UPDATE tce73 set b = 201 where a = 100; } } {1 {foreign key constraint failed}} do_test without_rowid3-ce7c13.1.6 { catchsql { UPDATE tce73 set a = 101 where a = 100; } } {1 {foreign key constraint failed}} finish_test |