/ Check-in [ad15486022]
Login

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

Overview
Comment:Fix a problem when renaming an IPK column that is also part of a child key.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256: ad15486022209205c65fb5ffdbe30a7b99379170451e6aff4bab6e90b549d6c7
User & Date: dan 2018-08-14 21:03:38
Context
2018-08-18
18:01
Have ALTER TABLE RENAME COLUMN also edit trigger and view definitions. check-in: 7908e8a4a3 user: dan tags: alter-table-rename-column
2018-08-14
21:05
Fix a problem when renaming an IPK column that is also part of a child key. check-in: 6e6a2bfdbb user: dan tags: edit-trigger-wrapper
21:03
Fix a problem when renaming an IPK column that is also part of a child key. check-in: ad15486022 user: dan tags: alter-table-rename-column
20:38
Do not allow ALTER TABLE RENAME COLUMN on a virtual table. check-in: f6d6b47271 user: drh tags: alter-table-rename-column
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

  1112   1112     sWalker.pParse = &sParse;
  1113   1113     sWalker.xExprCallback = renameColumnExprCb;
  1114   1114     sWalker.u.pRename = &sCtx;
  1115   1115   
  1116   1116     if( sParse.pNewTable ){
  1117   1117       int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
  1118   1118       FKey *pFKey;
         1119  +    for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
         1120  +      for(i=0; i<pFKey->nCol; i++){
         1121  +        if( bFKOnly==0 && pFKey->aCol[i].iFrom==sCtx.iCol ){
         1122  +          renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
         1123  +        }
         1124  +        if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
         1125  +         && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
         1126  +        ){
         1127  +          renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
         1128  +        }
         1129  +      }
         1130  +    }
  1119   1131       if( bFKOnly==0 ){
  1120   1132         renameTokenFind(
  1121   1133             &sParse, &sCtx, (void*)sParse.pNewTable->aCol[sCtx.iCol].zName
  1122   1134         );
  1123   1135         assert( sCtx.iCol>=0 );
  1124   1136         if( sParse.pNewTable->iPKey==sCtx.iCol ){
  1125   1137           renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
................................................................................
  1126   1138           sCtx.iCol = -1;
  1127   1139         }
  1128   1140         sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
  1129   1141         for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
  1130   1142           sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
  1131   1143         }
  1132   1144       }
  1133         -
  1134         -    for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
  1135         -      for(i=0; i<pFKey->nCol; i++){
  1136         -        if( bFKOnly==0 && pFKey->aCol[i].iFrom==sCtx.iCol ){
  1137         -          renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
  1138         -        }
  1139         -        if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
  1140         -         && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
  1141         -        ){
  1142         -          renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
  1143         -        }
  1144         -      }
  1145         -    }
  1146   1145     }else{
  1147   1146       sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
  1148   1147       sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
  1149   1148     }
  1150   1149   
  1151   1150     assert( nQuot>=nNew );
  1152   1151     zOut = sqlite3DbMallocZero(db, nSql + sCtx.nList*nQuot + 1);

Changes to test/altercol.test.

    67     67       {CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)}
    68     68   
    69     69    15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
    70     70       {CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}
    71     71   
    72     72    16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
    73     73       {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}
           74  +
           75  + 17  {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (b) REFERENCES t2)}
           76  +     {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (d) REFERENCES t2)}
    74     77   
    75     78   } {
    76     79     reset_db
    77     80     do_execsql_test 1.$tn.0 $before
    78     81   
    79     82     do_execsql_test 1.$tn.1 {
    80     83       INSERT INTO t1 VALUES(1, 2, 3);