sqlstreak

SQL error

null value in column "name" violates not-null constraint

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

A column declared NOT NULL received no value. Provide a value for it, or give the column a DEFAULT so inserts can leave it out.

The exact messages

  • PostgreSQLERROR: null value in column "name" of relation "pets" violates not-null constraint
  • SQLiteNOT NULL constraint failed: pets.name

How to read this message

This is what PostgreSQL 18.6 prints for the broken query below, the way psql shows it. DETAIL says more about what went wrong.

ERROR:  null value in column "name" of relation "pets" violates not-null constraint
DETAIL:  Failing row contains (21, null, dog, null, 2, 10.0, f, 2026-01-01).

SQLite 3.54.1 reports the same mistake as NOT NULL constraint failed: pets.name, with no line number: look for the word or column it names.

Why it happens

Explicitly inserting NULL

Often from application code passing an empty or undefined value.

Leaving a required column out of the INSERT

Columns you don't list get NULL unless they have a DEFAULT.

Reproduce it, then fix it

Both queries run on a real database in your browser. Switch dialects to compare.

Broken
INSERT INTO pets (id, name, species, age, weight_kg, adopted, arrived_on)
VALUES (21, NULL, 'dog', 2, 10, false, '2026-01-01');
Fixed
INSERT INTO pets (id, name, species, age, weight_kg, adopted, arrived_on)
VALUES (21, 'Rex', 'dog', 2, 10, false, '2026-01-01');
SELECT id, name FROM pets WHERE id = 21;

Every NOT NULL column now has a value.

The fix returns, PostgreSQL: 1 row

idname
21Rex

The fix returns, SQLite: 1 row

idname
21Rex

Experiment with it: PostgreSQL playground·SQLite playground

Official documentation

Stop hitting this error

Practise the concept behind it in a five-minute lesson, or read the full guide: SQL CREATE TABLE, constraints and views.