null-write¶
Default severity: error
What it does¶
Reports a NULL that an INSERT ... VALUES, an INSERT ... SELECT, an
UPDATE ... SET, an ON CONFLICT DO UPDATE SET or a MERGE action writes to a
column the schema declares NOT NULL. PostgreSQL only: SQLite and MySQL give some
NOT NULL columns a value for NULL (a rowid, an AUTO_INCREMENT). A table with a
BEFORE row trigger for the statement is not checked, since the trigger may replace
the value.
Why is this bad?¶
PostgreSQL rejects the statement with NotNullViolation at runtime.
Example¶
[tool.sqlty]
schema = "schema.sql"
schema.sql:
CREATE TABLE person (id INT PRIMARY KEY, first_name TEXT NOT NULL, nickname TEXT);
# error: [null-write] "`person.first_name` is NOT NULL, but NULL is written to it"
clear = "--sql\nUPDATE person SET first_name = NULL WHERE id = %s"
# error: [null-write] "`person.first_name` is NOT NULL, but NULL is written to it"
add = "--sql\nINSERT INTO person (id, first_name) VALUES (%s, NULL)"
Open in playgroundWrite a value, or drop NOT NULL from the column. A value that only may be NULL is
nullable-write.