sqlstreak

SQL error

there is no unique or exclusion constraint matching the ON CONFLICT specification

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

Short answer

ON CONFLICT (column) only works on columns covered by a PRIMARY KEY or UNIQUE constraint (or unique index): that's how the database knows what a conflict is. Point it at the key, or add the constraint first.

The exact messages

  • PostgreSQLERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
  • SQLiteON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint

How to read this message

This is what PostgreSQL 18.6 prints for the broken query below, the way psql shows it.

ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification

SQLite 3.54.1 reports the same mistake as ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint, with no line number: look for the word or column it names.

Why it happens

The column isn't unique

ON CONFLICT (name) when names can repeat. The database has no rule saying two rows can't share a name.

A different column list than the index

The list must match a unique constraint exactly: (a, b) doesn't match a unique index on (a) alone.

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, 'Mochi', 'cat', 2, 4.0, false, '2026-01-01')
ON CONFLICT (name) DO NOTHING;
SELECT COUNT(*) AS pets FROM pets;
Fixed
INSERT INTO pets (id, name, species, age, weight_kg, adopted, arrived_on)
VALUES (2, 'Mochi', 'cat', 3, 4.2, true, '2025-11-02')
ON CONFLICT (id) DO UPDATE SET age = excluded.age;
SELECT id, name, age FROM pets WHERE id = 2;

id is the primary key, so a second row with id 2 is a conflict the database can detect; the upsert updates Mochi's age instead.

The fix returns, PostgreSQL: 1 row

idnameage
2Mochi3

The fix returns, SQLite: 1 row

idnameage
2Mochi3

Experiment with it: PostgreSQL playground·SQLite playground

Another fix

If names really are unique, say so with a unique index; then ON CONFLICT (name) works (the duplicate is skipped: still 20 pets).

CREATE UNIQUE INDEX pets_name_key ON pets (name);
INSERT INTO pets (id, name, species, age, weight_kg, adopted, arrived_on)
VALUES (21, 'Mochi', 'cat', 2, 4.0, false, '2026-01-01')
ON CONFLICT (name) DO NOTHING;
SELECT COUNT(*) AS pets FROM pets;

Returns, PostgreSQL: 1 row

pets
20

Returns, SQLite: 1 row

pets
20

Official documentation

Stop hitting this error

Practise the concept behind it in a five-minute lesson, or read the full guide: SQL INSERT, UPDATE and DELETE.