Home › Databases

ERROR: null value in column violates not-null constraint (PostgreSQL) — how to fix

ERROR:  null value in column "id" of relation "users" violates not-null constraint
DETAIL:  Failing row contains (null, alice@example.com, 2025-01-10 12:00:00).

Quick fix: Read the column name and the DETAIL row. For an id with no default, add one: ALTER TABLE users ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY; then sync it: SELECT setval(pg_get_serial_sequence('users','id'), (SELECT max(id) FROM users));. For other columns, add a DEFAULT or pass a value, and don't send NULL/None for columns you want the database to fill.

What it means

The column is declared NOT NULL, and the row being written has NULL in it. Postgres tells you the column ("id"), the table (relation "users", shown since PostgreSQL 13), and the full failing row in DETAIL. Values appear in column order, so you can see exactly which one is null.

A column ends up NULL in two ways:

  1. You didn't mention it in the INSERT, and it has no default, so Postgres uses NULL.
  2. You (or the ORM) sent NULL explicitly. An explicit NULL is used as-is, even if the column has a default.

Check the column's default:

\d users
-- or
SELECT column_name, is_nullable, column_default, is_identity
FROM information_schema.columns WHERE table_name = 'users';

Cause 1: the id column has no auto-increment

Common after creating a table by hand as id integer PRIMARY KEY, or after copying a schema/dump without its sequences.

Fix: turn the column into an identity column (PostgreSQL 10+), then move the counter past the existing rows:

ALTER TABLE users ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY;
SELECT setval(pg_get_serial_sequence('users', 'id'),
              COALESCE((SELECT max(id) FROM users), 0) + 1, false);

Without the setval, the new identity starts at 1 and the next insert fails with duplicate key value violates unique constraint.

For new tables, declare it from the start:

CREATE TABLE users (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email text NOT NULL
);

Cause 2: the code sends NULL for an auto-generated column

# SQLAlchemy / psycopg: passing id=None sends NULL
cur.execute("INSERT INTO users (id, email) VALUES (%s, %s)", (None, email))

Fix: leave the column out of the INSERT so the default applies:

INSERT INTO users (email) VALUES ('alice@example.com') RETURNING id;

Or write the keyword DEFAULT instead of a value: VALUES (DEFAULT, 'alice@example.com'). In ORMs, check that the model marks the field as auto-generated (for example a primary key with autoincrement, or @default(autoincrement()) in Prisma) and that the database migration created a matching default.

Cause 3: a timestamp or status column without a default

The model fills created_at in application code, but a raw SQL insert, a script or another service doesn't.

Fix: let the database fill it:

ALTER TABLE users ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'pending';

Cause 4: the value really is missing

The form field, JSON key or CSV column is empty, so your code passes None/null/undefined. Validate input before writing, return a 400 to the client, or use COALESCE(%s, 'fallback') in the SQL if a fallback makes sense.

If the column should allow missing values, drop the constraint:

ALTER TABLE users ALTER COLUMN phone DROP NOT NULL;

ALTER TABLE ... SET NOT NULL fails with column "x" of relation "t" contains null values if rows already have NULLs. Backfill first:

UPDATE users SET phone = '' WHERE phone IS NULL;
ALTER TABLE users ALTER COLUMN phone SET NOT NULL;

Last updated 2026-12-04.