PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL error 42P10: there is no unique or exclusion constraint matching the ON CONFLICT specification

The columns in ON CONFLICT (…) do not exactly match any unique constraint or unique index on the table.

PostgreSQL 17.12
42P10: there is no unique or exclusion constraint matching the ON CONFLICT specification

Jump to a cause

  1. 01The ON CONFLICT column has no unique constraint
  2. 02The unique key has several columns but ON CONFLICT lists only some
  3. 03The unique index is partial (WHERE deleted_at IS NULL)
  4. 04The unique index is on an expression such as lower(email)

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

ON CONFLICT (columns) does not just name columns to compare. PostgreSQL looks for a unique constraint or unique index whose columns (and expression or WHERE condition) match exactly, and uses it as the arbiter. If none matches, the INSERT fails with 42P10 before any row is checked — even when there is no duplicate at all.

01The ON CONFLICT column has no unique constraint

users.email is a plain text column. The upsert ON CONFLICT (email) DO UPDATE has nothing to arbitrate on.

Setup
Setuprun as lab_a
CREATE TABLE users (id serial PRIMARY KEY, email text, name text);
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim');
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim Minsu')
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;
✕ ERROR
ERROR:  42P10: there is no unique or exclusion constraint matching the ON CONFLICT specification
Fixrun as lab_a
ALTER TABLE users ADD CONSTRAINT users_email_key UNIQUE (email);
OUTPUT
ALTER TABLE

Add UNIQUE (email). The same upsert then updates the existing row: Kim becomes Kim Minsu and there is still only one row. If the table already contains duplicate emails, the ALTER fails, so clean them up first.

After the fixrun as lab_a
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim Minsu')
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;
SELECT id, email, name FROM users;
✓ SUCCESS
INSERT 0 1
 id |      email      |   name
----+-----------------+-----------
  1 | kim@example.com | Kim Minsu
(1 row)

02The unique key has several columns but ON CONFLICT lists only some

The primary key is (warehouse_id, item_code). ON CONFLICT (item_code) alone does not match it — a subset of a composite key is not unique.

Setup
Setuprun as lab_a
CREATE TABLE stock (warehouse_id int, item_code text, qty int, PRIMARY KEY (warehouse_id, item_code));
INSERT INTO stock VALUES (1, 'A100', 5);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
INSERT INTO stock VALUES (1, 'A100', 3)
ON CONFLICT (item_code) DO UPDATE SET qty = stock.qty + EXCLUDED.qty;
✕ ERROR
ERROR:  42P10: there is no unique or exclusion constraint matching the ON CONFLICT specification
Checkrun as lab_a
SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'stock'::regclass;
OUTPUT
  conname   |         pg_get_constraintdef
------------+---------------------------------------
 stock_pkey | PRIMARY KEY (warehouse_id, item_code)
(1 row)

List every column of the key: ON CONFLICT (warehouse_id, item_code). The quantity becomes 5 + 3 = 8. pg_get_constraintdef shows the exact column list.

After the fixrun as lab_a
INSERT INTO stock VALUES (1, 'A100', 3)
ON CONFLICT (warehouse_id, item_code) DO UPDATE SET qty = stock.qty + EXCLUDED.qty;
SELECT * FROM stock;
✓ SUCCESS
INSERT 0 1
 warehouse_id | item_code | qty
--------------+-----------+-----
            1 | A100      |   8
(1 row)

03The unique index is partial (WHERE deleted_at IS NULL)

Email is unique only among non-deleted members. ON CONFLICT (email) does not match a partial index unless you repeat its condition.

Setup
Setuprun as lab_a
CREATE TABLE members (id serial PRIMARY KEY, email text, deleted_at timestamp);
CREATE UNIQUE INDEX members_email_active ON members (email) WHERE deleted_at IS NULL;
INSERT INTO members (email) VALUES ('lee@example.com');
OUTPUT
CREATE TABLE
CREATE INDEX
INSERT 0 1
Reproducerun as lab_a
INSERT INTO members (email) VALUES ('lee@example.com')
ON CONFLICT (email) DO NOTHING;
✕ ERROR
ERROR:  42P10: there is no unique or exclusion constraint matching the ON CONFLICT specification
Checkrun as lab_a
SELECT indexdef FROM pg_indexes WHERE tablename = 'members' AND indexname = 'members_email_active';
OUTPUT
                                                 indexdef
----------------------------------------------------------------------------------------------------------
 CREATE UNIQUE INDEX members_email_active ON lab_a.members USING btree (email) WHERE (deleted_at IS NULL)
(1 row)

Add the index predicate: ON CONFLICT (email) WHERE deleted_at IS NULL DO NOTHING. The duplicate is skipped (INSERT 0 0) and only one row remains.

After the fixrun as lab_a
INSERT INTO members (email) VALUES ('lee@example.com')
ON CONFLICT (email) WHERE deleted_at IS NULL DO NOTHING;
SELECT id, email FROM members;
✓ SUCCESS
INSERT 0 0
 id |      email
----+-----------------
  1 | lee@example.com
(1 row)

04The unique index is on an expression such as lower(email)

The index is on lower(email), not on email, so ON CONFLICT (email) does not match it.

Setup
Setuprun as lab_a
CREATE TABLE accounts (id serial PRIMARY KEY, email text, name text);
CREATE UNIQUE INDEX accounts_email_lower ON accounts (lower(email));
INSERT INTO accounts (email, name) VALUES ('Park@Example.com', 'Park');
OUTPUT
CREATE TABLE
CREATE INDEX
INSERT 0 1
Reproducerun as lab_a
INSERT INTO accounts (email, name) VALUES ('park@example.com', 'Park J')
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;
✕ ERROR
ERROR:  42P10: there is no unique or exclusion constraint matching the ON CONFLICT specification
Checkrun as lab_a
SELECT indexdef FROM pg_indexes WHERE indexname = 'accounts_email_lower';
OUTPUT
                                       indexdef
---------------------------------------------------------------------------------------
 CREATE UNIQUE INDEX accounts_email_lower ON lab_a.accounts USING btree (lower(email))
(1 row)

Write the same expression: ON CONFLICT (lower(email)). park@example.com now conflicts with the stored Park@Example.com and the name is updated to Park J; the stored email keeps its original case.

After the fixrun as lab_a
INSERT INTO accounts (email, name) VALUES ('park@example.com', 'Park J')
ON CONFLICT (lower(email)) DO UPDATE SET name = EXCLUDED.name;
SELECT id, email, name FROM accounts;
✓ SUCCESS
INSERT 0 1
 id |      email       |  name
----+------------------+--------
  1 | Park@Example.com | Park J
(1 row)

FAQ

How do I fix there is no unique or exclusion constraint matching the ON CONFLICT specification?

Make the ON CONFLICT target match a unique constraint or index exactly: same columns, same expression, and the same WHERE condition for a partial index. If there is none, add a UNIQUE constraint.

Can I use ON CONFLICT with a column that is not unique?

No. The conflict target must be backed by a unique constraint, a unique index or an exclusion constraint.

How do I use ON CONFLICT with a composite primary key?

List all key columns in the same set, for example ON CONFLICT (warehouse_id, item_code).

How do I use ON CONFLICT with a partial unique index?

Repeat the index condition after the column list: ON CONFLICT (email) WHERE deleted_at IS NULL.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12