Diagnostic Queries
Symptoms
A row supplied NULL for a column declared NOT NULL (commonly an identity/required column). PostgreSQL raises SQLSTATE 23502 (not_null_violation).
- A NOT NULL column received a NULL value.
- Common with identity columns set to OVERRIDING incorrectly, or missing values.
- The DETAIL line shows the failing row.
What the server log shows
ERROR: null value in column "id" of relation "orders" violates not-null constraint
DETAIL: Failing row contains (null, 2024-01-01, 42.00).
Why PostgreSQL raises this — what the manual says
Section 5.5.2 Not-Null Constraints:
“A not-null constraint simply specifies that a column must not assume the null value.”
A NOT NULL column rejects NULLs. Inserting/updating a row that leaves the column NULL (no value, an explicit NULL, or a misused identity override) violates the constraint, so PostgreSQL reports 23502.
Common causes
- Omitting a required column without a default.
- Explicitly inserting NULL into a NOT NULL column.
- Misusing
OVERRIDING SYSTEM VALUEor feeding NULL to an identity column.
How to fix it
- Provide a non-null value for the column.
- For identity columns, let the database generate the value (don’t pass NULL).
- Add a sensible
DEFAULTif appropriate.
Related & next steps
Reference: PostgreSQL 18 Section 5.3 “Constraints”.
Thanks — noted. This helps keep the database accurate.