22004
PostgreSQLERRORNotableData ExceptionHIGH confidence
null value not allowed
What this means
SQLSTATE 22004 is raised when a NULL value is provided to a function or context that explicitly requires a non-NULL value. This differs from 23502 (NOT NULL constraint violation on a column) — 22004 applies to function arguments and specific expression contexts.
Why it happens
- 1Passing NULL to a function parameter declared as STRICT (in PL/pgSQL STRICT functions return NULL immediately, but some contexts raise 22004)
- 2A domain with a NOT NULL constraint receives NULL
- 3Built-in functions that explicitly prohibit NULL arguments
How to reproduce
NULL value passed where explicitly not allowed.
trigger — this will error
trigger — this will error
CREATE DOMAIN positive_int AS INTEGER NOT NULL CHECK (VALUE > 0); INSERT INTO t (col) VALUES (NULL::positive_int);
expected output
ERROR: domain positive_int does not allow null values
Fix
Provide a non-NULL value or a default
WHEN When inserting or passing values to contexts that forbid NULL.
Provide a non-NULL value or a default
INSERT INTO t (col) VALUES (COALESCE(source_val, 1));
Why this works
COALESCE substitutes a default value when the source is NULL, preventing 22004.
Sources
Official documentation ↗
Class 22 — Data Exception
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev