Error AtlasError Documentation and Resolution

PostgreSQL 23502: null value violates a not-null constraint

null value in column "column_name" of relation "table_name" violates not-null constraint

SQLSTATE 23502 is not_null_violation. A required column received SQL NULL, either explicitly or through the insert, update, or migration path. A default does not replace an explicitly supplied NULL. Diagnose how the value was produced before relaxing a requirement that other code relies on.

null value in column "column_name" of relation "table_name" violates not-null constraint
  • The request omitted a required property, or an ORM converted an absent application value into an explicit SQL NULL.
  • An INSERT omitted a column that has no usable default, or an UPDATE assigned NULL after a failed lookup or transformation.
  • A deployment introduced a required column before all writers supplied it, or a migration tried to enforce NOT NULL while existing rows still contained nulls.
  1. Use the error column and table names to inspect the exact target with psql \d schema.table_name. Compare the generated SQL and bound parameters with the schema, redacting sensitive values rather than logging complete payloads.
  2. Distinguish missing, NULL, empty string, and zero in the application. Supply the real required value. If the database should generate it, omit the column or use DEFAULT where supported instead of passing NULL.
  3. Inspect information_schema.columns for column_default and is_nullable in the affected schema. A default expression can itself return NULL, so verify its behavior rather than assuming the presence of a default guarantees a value.
  4. For a migration failure, count affected rows with a scoped SELECT COUNT(*) FROM schema.table_name WHERE column_name IS NULL. Plan a valid backfill and compatible writer rollout before enforcing the requirement; review lock and table-scan costs on large tables.
  5. Add tests for omitted and explicit-null inputs, recover the failed transaction, and retry the corrected operation. Do not use arbitrary sentinel values or remove NOT NULL solely to suppress the error unless the data model genuinely allows missing information.
PostgreSQL documentation: SQLSTATE error codes