All rules Rule SL004 · CN
SQLite · beta
critical
CN · Constraints & keys
free in the CLI, --engine=sqlite

Can SQLite add a stored generated column to an existing table?

ADD COLUMN GENERATED ... STORED

Critical: SQLite refuses the statement, so the migration fails here.

What happens

A STORED generated column cannot be added with ALTER TABLE, SQLite only allows VIRTUAL generated columns there ("cannot add a STORED column"). The statement fails; nothing is added.

Why it is dangerous on a populated table

SQLite holds one write lock for the whole database, so anything that rebuilds a table blocks every writer for as long as the copy takes, and that time grows with the table.

Fires on

ALTER TABLE orders ADD COLUMN total_cents INTEGER GENERATED ALWAYS AS (qty * unit_cents) STORED;

The safe pattern

Declare the generated column VIRTUAL, which ALTER TABLE accepts and which costs nothing at write time. A STORED column belongs in CREATE TABLE, for an existing table that means a rebuild.

ALTER TABLE orders ADD COLUMN total_cents INTEGER GENERATED ALWAYS AS (qty * unit_cents) VIRTUAL;

Fixtures

The rule ships with these files and the test suite runs them on every change: the first set must fire, the second must stay silent.

Fires (1)

stored
ALTER TABLE orders ADD COLUMN total_cents INTEGER GENERATED ALWAYS AS (qty * unit_cents) STORED;

Stays silent (2)

virtual implicit
ALTER TABLE orders ADD COLUMN total_cents INTEGER AS (qty * unit_cents);
virtual
ALTER TABLE orders ADD COLUMN total_cents INTEGER GENERATED ALWAYS AS (qty * unit_cents) VIRTUAL;

How to check locally

Catch this before it ships

SQLite support is in beta: this rule runs locally in the free CLI, static only, and not in the hosted service yet. No install, nothing leaves your machine:

npx bolvrk check migration.sql --engine=sqlite