SQLSTATE 42830Severity mediumLab verified

Incident brief

No unique constraint for foreign key

A foreign key must reference columns backed by a PRIMARY KEY or UNIQUE constraint on the parent; referencing unconstrained columns raises this invalid-foreign-key error.

Reproduced on PostgreSQL 18.4Verified 2026-07-24 (isolated lab, PostgreSQL 18.4)Reviewed by Verified against PostgreSQL 18.4 in an isolated lab environment

In 10 seconds

What
No unique constraint for foreign key
What triggers it
Define a foreign key that references parent columns with no PRIMARY KEY or UNIQUE constraint.
The fix
Add a PRIMARY KEY or UNIQUE constraint on the referenced parent columns first.
Proof
Reproduced on PostgreSQL 18.4 → A foreign-key definition reproduces SQLSTATE 42830 because the parent columns are not unique.

The fix

What to do right now

The immediate, application-level response to this error.

  • Add a PRIMARY KEY or UNIQUE constraint on the referenced parent columns first.
  • Then create the foreign key so it points at the now-unique target.
Fix — SQL
-- Back the referenced columns with a unique constraint first.
ALTER TABLE fk_parent ADD PRIMARY KEY (a);
CREATE TABLE fk_child (parent_ref int REFERENCES fk_parent(a));

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 18.4
Last verified
2026-07-24 (isolated lab, PostgreSQL 18.4)
Reviewed by
Verified against PostgreSQL 18.4 in an isolated lab environment
Audit status
reviewed