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

Source: https://sqlstreak.com/sql/errors/no-unique-constraint-matching-on-conflict · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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

- PostgreSQL: `ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification`
- SQLite: `ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint`

## How PostgreSQL prints it

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

## 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.

## The broken query

```sql
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;
```

## The fix

```sql
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.

| id | name | age |
| --- | --- | --- |
| 2 | Mochi | 3 |
