literal-write¶
Default severity: error
What it does¶
Reports a constant that an INSERT ... VALUES, an INSERT ... SELECT, an
UPDATE ... SET (a tuple SET (a, b) = (x, y) too) or an ON CONFLICT DO UPDATE SET
writes to a column whose values the schema lists, as an enum or a
CHECK (column IN (...)), when the constant is not one of them.
[tool.sqlty]
schema = "schema.sql"
schema.sql:
CREATE TYPE pay_kind AS ENUM ('card', 'iban');
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
status TEXT NOT NULL CHECK (status IN ('pending', 'paid')),
kind pay_kind
);
# error: [literal-write] "`orders.status` is 'pending' or 'paid', not 'refunded'"
refund = "--sql\nUPDATE orders SET status = 'refunded' WHERE id = %s"
# error: [literal-write] "`orders.status` is 'pending' or 'paid', not 'refunded'"
add = "--sql\nINSERT INTO orders (status) VALUES ('refunded')"
# error: [literal-write] "`orders.kind` is 'card' or 'iban', not 'cash'"
cash = "--sql\nINSERT INTO orders (status, kind) VALUES ('paid', 'cash')"
Open in playgroundWhy is this bad?¶
The database rejects the row: an enum has no such label, and the CHECK fails. It
fails at run time, after the deploy. A migration that removes a value breaks every
query that still writes it, which sqlty breaking reports in the backward direction.
How to fix¶
Write one of the listed values, or add the value to the enum or the CHECK first:
# error:
refund = "--sql\nUPDATE orders SET status = 'paid' WHERE id = %s"
Open in playgroundConfiguration¶
An error by default: the database rejects such SQL. Lower or turn it off:
[tool.sqlty.rules]
literal-write = "warn"
Needs a schema, and is judged in the postgres
and sqlite dialects, where comparison is exact. Only constants are checked: a
parameter, an expression or a column a query selects is not. A WHERE that compares a
column with a value it never holds is unmatched-literal.