The rebuild that ate the rules
Rebuilding a SQLite table used to cost you the rules that table enforced. Version 4.2 of sqlite-utils, which automates the rebuild, finally carries every one of them across.
SQLite will rename a column for you, or add one. It will not change a column's type or attach a new rule to it, and SQLite's own manual answers that request with a twelve-step procedure: build a second table with the columns you wanted, copy every row across, drop the original, rename the replacement into its place, then rebuild the indexes, triggers and views that were pointing at the table you just deleted. Simon Willison's sqlite-utils has performed that whole dance behind a single function call since 2020. On 13 August he shipped the version that finally performs it without dropping anything on the floor.
The same release is the first that can list the rules a table enforces. Until now the library had no structured way to read the constraints it was throwing away.
What it used to drop were the table's own guarantees. A table can carry rules the database itself enforces: no two rows may share this value, this number must be positive, this column may never be empty. Rebuild the table to widen one column and those rules did not come across. Every row arrived; none of the promises did. Nothing raised an error and nothing looked different, so the table went on quietly accepting whatever it was handed. The counter behind an auto-numbered ID column reset too, which meant new rows could start reusing numbers the table had already issued.
Version 4.2 carries all of it: the uniqueness rules with their names, sort order and text-comparison settings; the conditions a column's values must satisfy; the ID counter's memory of the highest number it ever issued; the indexes on a renamed column; and the comments a developer left beside a column, which now follow that column when it is renamed and vanish when it is deleted. The renaming is the nice part. When a rule mentions a column by name and you rename that column, the rebuild rewrites the name inside the rule while leaving alone any piece of text or function that happens to be spelled identically. It reads the SQL well enough to tell a column from a coincidence.
This is unglamorous work, and it is the kind that decides whether a tool can be aimed at a database somebody cares about. A rebuild that silently deletes a table's rules is worse than one that refuses to run, because the consequence keeps its appointment for months and then arrives wearing someone else's name. That operation is now safe to point at a table you did not write yourself.
pip install -U sqlite-utils gets you 4.2.1 rather than the broken 4.2, and the changelog linked from the release post names, line by line, every part of a schema the rebuild now carries across.
Get the tool at simonwillison.net →The lenses
The facts
Concepts
How this connects
Tap a node to open it