SQLite

Check-in [ae9638e9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:When a table is renamed using "ALTER TABLE RENAME TO", update any REFERENCES clauses that refer to the table, unless "PRAGMA legacy_alter_table" is true and "PRAGMA foreign_keys" is set to false (i.e. so that when "PRAGMA legacy_alter_table" is set behaviour is still compatible with versions 3.24 and earlier).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ae9638e9c0ad0c366f93c88a850f6b4cc86881e9f3f9f1e39574d9d83ddd8a6a
User & Date: dan 2018-11-09 20:04:05
Context
2018-11-09
23:41
Fix invalid use of unprotected sqlite3_value objects in the sqldiff utility, when using the --changeset option. (check-in: a8d65214 user: drh tags: trunk)
20:04
When a table is renamed using "ALTER TABLE RENAME TO", update any REFERENCES clauses that refer to the table, unless "PRAGMA legacy_alter_table" is true and "PRAGMA foreign_keys" is set to false (i.e. so that when "PRAGMA legacy_alter_table" is set behaviour is still compatible with versions 3.24 and earlier). (check-in: ae9638e9 user: dan tags: trunk)
14:17
Fix for ticket [787fa716be3a7f650cac] (check-in: 531eca61 user: drh tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/alter.c.

1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
            sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
            if( sParse.nErr ) rc = sParse.rc;
            sqlite3WalkSelect(&sWalker, pTab->pSelect);
          }
        }else{
          /* Modify any FK definitions to point to the new table. */
#ifndef SQLITE_OMIT_FOREIGN_KEY
          if( db->flags & SQLITE_ForeignKeys ){
            FKey *pFKey;
            for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
              if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
                renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
              }
            }
          }







|







1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
            sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
            if( sParse.nErr ) rc = sParse.rc;
            sqlite3WalkSelect(&sWalker, pTab->pSelect);
          }
        }else{
          /* Modify any FK definitions to point to the new table. */
#ifndef SQLITE_OMIT_FOREIGN_KEY
          if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
            FKey *pFKey;
            for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
              if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
                renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
              }
            }
          }

Changes to test/altertab2.test.

36
37
38
39
40
41
42












































43
44
45
46
  }

  do_execsql_test 1.2 {
    INSERT INTO rr VALUES('in', 'tcl');
    SELECT * FROM ffff;
  } {hello world in tcl}
}














































finish_test








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




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
  }

  do_execsql_test 1.2 {
    INSERT INTO rr VALUES('in', 'tcl');
    SELECT * FROM ffff;
  } {hello world in tcl}
}

#-------------------------------------------------------------------------
# Check that table names that appear in REFERENCES clauses are updated
# when a table is renamed unless:
#
#   a) "PRAGMA legacy_alter_table" is true, and
#   b) "PRAGMA foreign_keys" is false.
#
do_execsql_test 2.0 {
  CREATE TABLE p1(a PRIMARY KEY, b);
  CREATE TABLE c1(x REFERENCES p1);
  CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES p1);
  CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES p1(a));
}

do_execsql_test 2.1 {
  ALTER TABLE p1 RENAME TO p2;
  SELECT sql FROM sqlite_master WHERE name LIKE 'c%';
} {
  {CREATE TABLE c1(x REFERENCES "p2")}
  {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p2")}
  {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p2"(a))}
}

do_execsql_test 2.2 {
  PRAGMA legacy_alter_table = 1;
  ALTER TABLE p2 RENAME TO p3;
  SELECT sql FROM sqlite_master WHERE name LIKE 'c%';
} {
  {CREATE TABLE c1(x REFERENCES "p2")}
  {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p2")}
  {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p2"(a))}
}

do_execsql_test 2.3 {
  ALTER TABLE p3 RENAME TO p2;
  PRAGMA foreign_keys = 1;
  ALTER TABLE p2 RENAME TO p3;
  SELECT sql FROM sqlite_master WHERE name LIKE 'c%';
} {
  {CREATE TABLE c1(x REFERENCES "p3")}
  {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p3")}
  {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p3"(a))}
}


finish_test