Course outline

SQLite / Advanced: concurrency, recovery, and application boundaries / Lesson 21

Generated columns and safe schema evolution

Model a same-row derived value and plan compatibility and migration checks.

About 4 min readContent reviewed
On this page

Lesson goals

  • Explain generated expressions and write restrictions.
  • Compare VIRTUAL and STORED columns and inspect schema metadata.
  • Plan a bounded migration and verify derived values.

Define the derived value

A line total in minor currency units can be derived from quantity and unit price on the same row. A generated column makes that relationship part of the schema rather than asking each writer to supply a matching total. The example uses deliberately small bounded integers in a disposable lab; real monetary contracts also need a currency, rounding rules, domain limits, and overflow analysis.

Create a stored calculation

The fixture creates a STORED generated value and inserts only ordinary columns. SQLite computes the value when the row is written. The declared checks constrain these demonstration inputs; they do not implement discounts or tax. Use a new disposable database and a SQLite library that supports generated columns. This SQL is illustrative and does not run against a configured application.

sql Derive a line total from the same row
CREATE TABLE lab_line (
  id INTEGER PRIMARY KEY,
  quantity INTEGER NOT NULL CHECK (quantity BETWEEN 1 AND 1000),
  unit_cents INTEGER NOT NULL CHECK (unit_cents BETWEEN 0 AND 100000),
  line_cents INTEGER GENERATED ALWAYS AS (quantity * unit_cents) STORED
);
INSERT INTO lab_line (id, quantity, unit_cents) VALUES (1, 3, 250);
SELECT id, quantity, unit_cents, line_cents FROM lab_line WHERE id = 1;

For the inserted row, three units at 250 minor units each produce line_cents 750. The generated value is read like another column but is not supplied directly in the INSERT. The checks are for this small fixture, not a universal money model.

Update inputs and inspect the schema

A writer changes the input columns rather than assigning the generated value. After the update, inspect the row and the schema. SQLite exposes generated columns through table_xinfo; ordinary table_info does not list them. Applications and migration tools that inspect schemas should use an appropriate metadata interface and account for the SQLite version they actually ship.

sql Recompute and inspect
UPDATE lab_line SET quantity = 4 WHERE id = 1;
SELECT id, quantity, unit_cents, line_cents FROM lab_line WHERE id = 1;
PRAGMA table_xinfo(lab_line);

The new quantity is four, so the generated total is 1000. The metadata output depends on the SQLite build and is not reproduced here. It includes a row for the generated column when supported; inspect its documented fields rather than assuming a fixed display layout.

Choose storage and expression

VIRTUAL values are computed when read, whereas STORED values are computed when the row is written and occupy database space. Both can participate in indexes, but costs depend on the workload. Expressions are restricted to same-row columns, literals, and scalar deterministic functions; subqueries, aggregate functions, and window functions are not permitted. Test any application-defined deterministic function consistently in every relevant runtime.

Rehearse the migration

SQLite cannot add a STORED generated column with ALTER TABLE ADD COLUMN; adding a VIRTUAL column has its own documented restrictions. Check the library version used by every reader, writer, and maintenance tool before changing a shared file. Rehearse a documented table-rebuild migration on an isolated copy, retain a tested backup, and verify row counts, constraints, indexes, triggers, foreign keys, and representative calculated values before deployment.

Common errors and fixes

cannot INSERT into generated column

Why it happens
The INSERT attempts to provide a value for a generated column.
How to fix it
Insert ordinary input columns and let SQLite calculate the generated value.
Prevent it next time
Name writable columns explicitly in application statements.

A migration cannot add a STORED column with ADD COLUMN.

Why it happens
SQLite does not support that ALTER TABLE form for STORED generated columns.
How to fix it
Design and rehearse a supported table rebuild when STORED is required.
Prevent it next time
Review migration limitations before choosing storage.

A schema inspection misses the derived column.

Why it happens
The tool only reads PRAGMA table_info.
How to fix it
Use table_xinfo where appropriate and handle its documented metadata.
Prevent it next time
Test schema tooling against generated-column fixtures.

2 practice exercises

1. After changing the fixture quantity to four, what is line_cents?

Show worked solution

It is 1000: four times 250. The stored generated value is computed from the changed inputs as part of the row write; no separate application assignment is needed.

2. Can an aggregate over other rows be placed in this generated expression?

Show worked solution

No. SQLite restricts generated expressions to the same row and permits scalar deterministic functions, not aggregates or subqueries. Model a cross-row total separately and define how it is maintained and checked.

Key takeaways

  • A generated value derives from the same row and cannot be directly written.
  • VIRTUAL and STORED differ in calculation time and storage.
  • Test compatibility and the full migration on an isolated copy.

Official references