SQL linting for database apps¶
A query can parse, run and still be wrong: a join that counts each order once per
item, a DELETE that lost its WHERE, a NULL written to a NOT NULL column, a
tenant table read without the tenant filter. The database accepts all of them.
sqlty check reads your schema and reports them where the query is written, in the
editor, a pre-commit hook or CI. It runs no query and needs no database.
Types are one part of it. sqlty has over 80 rules, and these catch mistakes that no type checker sees:
| Mistake | Rules | Default |
|---|---|---|
| Totals a join inflates | fanout-aggregate, chasm-join, unproven-join |
warn, ignore |
| Joins on the wrong key | foreign-key-mismatch, entity-mismatch |
warn, ignore |
| Grouping the database rejects | ungrouped-column, position-out-of-range |
error |
| Writes that touch every row | missing-where |
warn |
| Writes the schema forbids | null-write, nullable-write, insert-missing-column, literal-write |
error, warn |
| Filters that never match | unmatched-literal |
warn |
| Tenant data read across tenants | tenant-scope, tenant-policy |
error, warn |
| Schema drift | unknown-column, unknown-table, deprecated-column |
error, warn |
| Queries that break when the schema grows | select-star, ambiguous-column |
warn, error |
| Parameters that don't fit | param-count-mismatch, param-type-mismatch, extra-param |
error, warn |
| Migrations that lock or fail | 17 migration rules | error, warn |
An example¶
[tool.sqlty]
schema = "schema.sql"
schema.sql:
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('active', 'banned')));
CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users, total NUMERIC NOT NULL);
CREATE TABLE order_items (id INTEGER PRIMARY KEY, order_id INTEGER NOT NULL REFERENCES orders, amount NUMERIC NOT NULL);
import decimal
from sqlty import SQL
# error: [fanout-aggregate] "SUM(o.total) aggregates orders, whose rows repeat once per order_items row"
revenue = SQL[tuple[str, decimal.Decimal]]("""--sql
SELECT u.email, SUM(o.total)
FROM users u
JOIN orders o ON o.user_id = u.id
JOIN order_items i ON i.order_id = o.id
GROUP BY u.email""")
# error: [unmatched-literal] "`users.status` is never 'blocked', so this WHERE matches no row"
blocked = SQL[tuple[int]]("--sql\nSELECT id FROM users WHERE status = 'blocked'")
# error: [missing-where] "`UPDATE` without `WHERE` changes every row of `orders`: add a `WHERE`, or `WHERE true` to mean it"
reset = SQL[tuple[()]]("--sql\nUPDATE orders SET total = 0")
# error: [null-write] "`users.email` is NOT NULL, but NULL is written to it"
signup = SQL[tuple[()]]("--sql\nINSERT INTO users (id, email, status) VALUES (1, NULL, 'active')")
Open in playgroundThe fixes: aggregate the items per order first, compare status with a value
its CHECK allows, add the WHERE, write an email. A rule's page
explains why it matters and when to turn it off, and sqlty rule NAME prints it.
Where it runs¶
- Editor: the VS Code extension and the language server underline each finding as you type, with quick fixes.
- CI and pre-commit:
sqlty check .exits non-zero on errors (CI and pre-commit). - Coding agents: the check needs no database, so an agent can run it in its
sandbox and read
--output-format json(agents). - TypeScript and Go: the same rules through the ESLint plugin
and
go vet.
Severities are set per rule in [tool.sqlty.rules]; see
configuration. For layout and style, the
sqruff rule adds sqruff's lint rules and
sqlty format formats each query.