PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 42830: there is no unique constraint matching given keys for referenced table

The column a foreign key points to is not covered by a PRIMARY KEY or UNIQUE constraint.

PostgreSQL 17.12
42830: there is no unique constraint matching given keys for referenced table "customers"

Jump to a cause

  1. 01The parent table has no PRIMARY KEY
  2. 02The FK points to a column that is not unique (email)
  3. 03Referencing only one column of a composite primary key

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

A foreign key in PostgreSQL can only reference columns that have a PRIMARY KEY or UNIQUE constraint on exactly that column list. If the parent table has no such constraint, CREATE TABLE ... REFERENCES (or ALTER TABLE ... ADD FOREIGN KEY) fails with 42830 and the child table is not created.

The three runs below show the usual causes: the parent table has no primary key at all, the FK points to a normal column such as email, or the parent has a composite key and the FK uses only one of its columns.

In every case the fix was on the constraint side: add the missing key to the parent, or make the FK match the existing key.

01The parent table has no PRIMARY KEY

customers was created as (id int, email text NOT NULL) without a primary key. REFERENCES customers (id) failed with 42830.

Checking pg_constraint for customers returned 0 rows. After ALTER TABLE customers ADD PRIMARY KEY (id), the same CREATE TABLE orders succeeded.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int, email text NOT NULL);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers (id));
✕ ERROR
ERROR:  42830: there is no unique constraint matching given keys for referenced table "customers"
Checkrun as lab_a
SELECT conname, contype FROM pg_constraint WHERE conrelid = 'customers'::regclass;
OUTPUT
 conname | contype
---------+---------
(0 rows)
Fixrun as lab_a
ALTER TABLE customers ADD PRIMARY KEY (id);
OUTPUT
ALTER TABLE
After the fixrun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers (id));
✓ SUCCESS
CREATE TABLE

02The FK points to a column that is not unique (email)

customers had id as its primary key, but the new table referenced customers (email). email had no UNIQUE constraint, so it failed with 42830.

A query for duplicated emails returned 0 rows, so ADD CONSTRAINT customers_email_key UNIQUE (email) was added and the CREATE TABLE succeeded.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, email text NOT NULL);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
CREATE TABLE newsletters (id int PRIMARY KEY, customer_email text REFERENCES customers (email));
✕ ERROR
ERROR:  42830: there is no unique constraint matching given keys for referenced table "customers"
Checkrun as lab_a
SELECT email, count(*) FROM customers GROUP BY email HAVING count(*) > 1;
OUTPUT
 email | count
-------+-------
(0 rows)
Fixrun as lab_a
ALTER TABLE customers ADD CONSTRAINT customers_email_key UNIQUE (email);
OUTPUT
ALTER TABLE

If that duplicate check returns rows, adding UNIQUE fails until the duplicates are cleaned up.

After the fixrun as lab_a
CREATE TABLE newsletters (id int PRIMARY KEY, customer_email text REFERENCES customers (email));
✓ SUCCESS
CREATE TABLE

03Referencing only one column of a composite primary key

order_items has PRIMARY KEY (order_id, line_no). REFERENCES order_items (order_id) uses only one of the two columns, which is not unique by itself, so it failed with 42830.

pg_get_constraintdef showed the key as PRIMARY KEY (order_id, line_no). Adding line_no to the child and writing FOREIGN KEY (order_id, line_no) REFERENCES order_items (order_id, line_no) worked.

Setup
Setuprun as lab_a
CREATE TABLE order_items (order_id int, line_no int, product text, PRIMARY KEY (order_id, line_no));
OUTPUT
CREATE TABLE
Reproducerun as lab_a
CREATE TABLE shipments (id int PRIMARY KEY, order_id int REFERENCES order_items (order_id));
✕ ERROR
ERROR:  42830: there is no unique constraint matching given keys for referenced table "order_items"
Checkrun as lab_a
SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'order_items'::regclass;
OUTPUT
     conname      |      pg_get_constraintdef
------------------+---------------------------------
 order_items_pkey | PRIMARY KEY (order_id, line_no)
(1 row)
After the fixrun as lab_a
CREATE TABLE shipments (id int PRIMARY KEY, order_id int, line_no int, FOREIGN KEY (order_id, line_no) REFERENCES order_items (order_id, line_no));
✓ SUCCESS
CREATE TABLE

FAQ

How do I fix there is no unique constraint matching given keys for referenced table?

Give the referenced column(s) a PRIMARY KEY or UNIQUE constraint, or change the FK to use the parent's existing key columns.

Does the column need to be the primary key?

No. In the second case email was not the primary key; a UNIQUE constraint on it was enough.

The parent has a composite primary key. How do I reference it?

Reference all key columns together: FOREIGN KEY (order_id, line_no) REFERENCES order_items (order_id, line_no).

How do I see which keys the parent table has?

SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'order_items'::regclass; lists them.

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