alter col leaves sqlitesequence table behind note that i'm unsure of alter column rules,…
- User
alter col leaves sqlite_sequence table behind note that i'm unsure of alter column rules, i was not able to find any docs or rules around this, but my expectation is when we change a autoincrement col to plain text there shouldn't be a stale sqlite_sequence.
turso> CREATE TABLE t(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); INSERT INTO t(b) VALUES('x'),('y'); ALTER TABLE t ALTER COLUMN a TO a2 TEXT; SELECT * FROM sqlite_sequence; SELECT sql FROM sqlite_schema WHERE name='t'; ┌──────┬─────┐ │ name │ seq │ ├──────┼─────┤ │ t │ 2 │ └──────┴─────┘ ┌──────────────────────────────────┐ │ sql │ ├──────────────────────────────────┤ │ CREATE TABLE t (a2 TEXT, b TEXT) │ └──────────────────────────────────┘ turso> select * from sqlite_schema; ┌───────┬─────────────────┬─────────────────┬──────────┬────────────────────────────────────────┐ │ type │ name │ tbl_name │ rootpage │ sql │ ├───────┼─────────────────┼─────────────────┼──────────┼────────────────────────────────────────┤ │ table │ sqlite_sequence │ sqlite_sequence │ 2 │ CREATE TABLE sqlite_sequence(name,seq) │ ├───────┼─────────────────┼─────────────────┼──────────┼────────────────────────────────────────┤ │ table │ t │ t │ 3 │ CREATE TABLE t (a2 TEXT, b TEXT) │ └───────┴─────────────────┴─────────────────┴──────────┴────────────────────────────────────────┘Interface
No new interfaces are introduced.
- Assistant
Investigating how ALTER COLUMN handles AUTOINCREMENT and
sqlite_sequencecleanup. - Assistant
Reproducing the bug and checking SQLite behavior:
- Assistant
Implementing the fix: adding sqlite_sequence cleanup and clearing
has_autoincrement. - Assistant
Adding a test and running it:
- Assistant
Fixed.
ALTER COLUMNthat removesINTEGER PRIMARY KEY AUTOINCREMENT(e.g. changing it to plainTEXT) left a stalesqlite_sequencerow and kepthas_autoincrementset, so later inserts still updated the sequence counter.Changes:
-
core/translate/alter.rs— Addedemit_delete_sqlite_sequence_entry(same pattern as drop-table cleanup) and call it when anALTER COLUMNremoves the rowid-alias/autoincrement column. -
core/vdbe/execute.rs— Inop_alter_column, clearhas_autoincrementand drop the column fromprimary_key_columnswhen the altered column stops being a rowid alias. -
testing/sqltests/turso-tests/alter_column.sqltest— Addedalter-column-removes-autoincrement-clears-sqlite-sequence.
After the fix:
CREATE TABLE t(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); INSERT INTO t(b) VALUES('x'),('y'); ALTER TABLE t ALTER COLUMN a TO a2 TEXT; SELECT * FROM sqlite_sequence; -- empty INSERT INTO t(b) VALUES('z'); SELECT * FROM sqlite_sequence; -- still empty -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.