SQLite Forum

Bug: ALTER TABLE RENAME COLUM TO vs CTE VIEWs
Login
I think this is a logic error in your SQL.  I think SQLite is doing the
right thing.

When you rename t0.col1 to t0.col2, the view is transformed from this:

> ~~~~
CREATE VIEW v0 AS
  WITH n AS (SELECT t0.col1 FROM t0 ),
       o AS (SELECT col1 FROM n)
  SELECT * FROM o
;
~~~~

Into this:

> ~~~~
CREATE VIEW v0 AS
  WITH n AS (SELECT t0.col2 FROM t0 ),
       o AS (SELECT col1 FROM n)
  SELECT * FROM o
;
~~~~

Because you have not defined the column names of CTE "n", names are selected
by SQLite.  Formerly, SQLite was choosing "col1" as the name of the only
column in "n".  After the ALTER TABLE it chooses "col2".  SQLite is free to
choose any column name it wants for "n", since you haven't specified one.
But later, in the "o" CTE, you assume that the column name for "n" is "col1".