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.

What lands in your log

ERROR: there is no unique constraint matching given keys for referenced table "fk_parent"

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

In 10 seconds

What triggers it
Define a foreign key that references parent columns with no PRIMARY KEY or UNIQUE constraint.
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.

Fix

What to do right now

Application-level steps for 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
ALTER TABLE fk_parent ADD CONSTRAINT fk_parent_a_key UNIQUE (a);
CREATE TABLE fk_child (parent_ref int REFERENCES fk_parent(a));

Runbook to fix this

The runbook for this incident

Full step-by-step fixes for the condition behind this error: the diagnosis, the exact SQL, and output captured in the lab.

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops