Summary
Alter table rename <oldname> to <newname> fails if the database contains a view 
which selects from a nonexistent table.
Seen on sqlite 3.27.2.
Script to reproduce it:
create view view_1 as select * from table_1;
create table table_2 (col_1 text, col_2 text);
alter table table_2 rename to table_3;

The problem seems to be a side-effect of the change introduced in version 
3.25.0 to ALTER TABLE which made it update references to the renamed table in 
triggers and views.
renameTableFunc walks through the schema looking for views and triggers which 
reference the renamed table. It overwrites the schema entries unconditionally 
even if it has not changed them. I don't know why you do this unnecessary work, 
but I assume there is a reason. However it then calls sqlite3SelectPrep for 
each entry. This is definitely wrong, since sqlite3SelectPrep apparently tries 
to populate the view and fails in sqlite3LocateTable at line 106955. Views are 
a bit like the box containing Schrödinger's cat: you don't know what is inside 
until you look, so sqlite shouldn't look until the user explicitly asks it do 
so.
You could argue that users shouldn't create a view that selects from a 
nonexistent table, but sqlite currently allows it (and also allows you to drop 
the table). Banning such views would break a lot of existing scripts: many of 
mine modify a table in ways which ALTER TABLE can't do by creating a new table, 
dropping the original one, and renaming the new table as the old one. This 
paradigm is already broken by the change in the semantics of ALTER TABLE, but I 
can reinstate the old behaviour with a PRAGMA. However not allowing views on 
nonexistent tables would break it more thoroughly.

Richard Parkins
http://www.zen224037.zen.co.uk

rparkins999/sqliteman

| 
| 
| 
|  |  |

 |

 |
| 
|  | 
rparkins999/sqliteman

http://sqliteman.com/. Contribute to rparkins999/sqliteman development by 
creating an account on GitHub.
 |

 |

 |



_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to