Fix a problem with foreign keys and generated columns discovered by
Manuel Rigger.

FossilOrigin-Name: 27c0fdab1ba4d4993b164eb4b777c63e82aa247c3fa406121dc8ed94970a0b35
diff --git a/src/build.c b/src/build.c
index 5026b89..46bf367 100644
--- a/src/build.c
+++ b/src/build.c
@@ -947,13 +947,15 @@
 ** the end.
 **
 ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
-** this routine is a no-op macro.
+** this routine is a no-op macro.  If the pTab does not have any virtual
+** columns, then this routine is no-op that always return iCol.  If iCol
+** is negative (indicating the ROWID column) then this routine return iCol.
 */
 i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
   int i;
   i16 n;
   assert( iCol<pTab->nCol );
-  if( (pTab->tabFlags & TF_HasVirtual)==0 ) return iCol;
+  if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
   for(i=0, n=0; i<iCol; i++){
     if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
   }
diff --git a/src/expr.c b/src/expr.c
index 52b68b7..3b2b513 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -4136,7 +4136,7 @@
       Table *pTab = pExpr->y.pTab;
       int iCol = pExpr->iColumn;
       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 
-                     + (iCol>=0 ? sqlite3TableColumnToStorage(pTab, iCol) : -1);
+                     + sqlite3TableColumnToStorage(pTab, iCol);
 
       assert( pExpr->iTable==0 || pExpr->iTable==1 );
       assert( iCol>=-1 && iCol<pTab->nCol );