Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Devops
  3. Question
Devops

What is causing ERROR: there is no unique constraint matching given keys for referenced table?

Asked by Julian Springer Jul 19, 2021 24.4K views 3 answers
Share

About this question

Below example table structure gives an ERROR: there is no unique constraint matching given keys for the referenced table, and having stared at it for while now I can't figure out why this error arises in this situation.

BEGIN;
CREATE TABLE foo (
    name                VARCHAR(256) PRIMARY KEY
);
CREATE TABLE bar(
    pkey        SERIAL PRIMARY KEY,
    foo_fk      VARCHAR(256) NOT NULL REFERENCES foo(name), 
    name        VARCHAR(256) NOT NULL, 
    UNIQUE (foo_fk,name)
);
CREATE TABLE baz(   
    pkey            SERIAL PRIMARY KEY,
    bar_fk          VARCHAR(256) NOT NULL REFERENCES bar(name),
    name            VARCHAR(256)
);
COMMIT;

Running the above code gives the following error, which does not make sense to me, can anyone explain why this error arises. I am using Postgres 9.1

NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey" for table "foo"
NOTICE:  CREATE TABLE will create implicit sequence "bar_pkey_seq" for serial column "bar.pkey"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "bar_pkey" for table "bar"
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "bar_foo_fk_name_key" for table "bar"
NOTICE:  CREATE TABLE will create implicit sequence "baz_pkey_seq" for serial column "baz.pkey"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "baz_pkey" for table "baz"
ERROR:  there is no unique constraint matching given keys for referenced table "bar"

********** Error **********

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

SQL state: 42830

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 3, 2025

The "ERROR: there is no unique constraint matching given keys for referenced table" in PostgreSQL occurs when creating a foreign key constraint, but the referenced column is not unique or not a primary key.

Causes of the Error

1. Referencing a Non-Unique Column

  • When creating a foreign key, PostgreSQL requires the referenced column to be a primary key or have a unique constraint.

Example of a failing scenario:

CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    customer_name TEXT
);
CREATE TABLE invoices (
    invoice_id SERIAL PRIMARY KEY,
    customer_name TEXT REFERENCES orders(customer_name)  -- Error occurs here
);

customer_name is not unique in orders, so it cannot be a foreign key reference.

2. Missing a Unique Constraint on the Referenced Column

The referenced column must have either a PRIMARY KEY or UNIQUE constraint.

Solution: Add a unique constraint.

  ALTER TABLE orders ADD CONSTRAINT unique_customer UNIQUE(customer_name);

OR

CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    customer_name TEXT UNIQUE  -- Now it's a valid reference
);

3. Referencing a Composite Key Without a Unique Constraint

If referencing multiple columns, they must form a composite unique constraint.

CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    product_id INT,
    customer_id INT,
    UNIQUE (product_id, customer_id)
);
CREATE TABLE invoices (
    invoice_id SERIAL PRIMARY KEY,
    product_id INT,
    customer_id INT,
    FOREIGN KEY (product_id, customer_id) REFERENCES orders(product_id, customer_id)
);

How to Fix

✔ Ensure the referenced column has a PRIMARY KEY or UNIQUE constraint.

✔ Use composite unique constraints if referencing multiple columns.

✔ Check for typos in column names when defining foreign keys.

Let me know if you need more details!

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 23, 2024

The error "ERROR: There is no unique constraint matching given keys for referenced table" typically occurs when trying to create a foreign key constraint in a database table, but the referenced column(s) do not have a unique constraint defined in the referenced table.


To resolve this issue, you can either:

  1. Add a unique constraint to the referenced column(s) in the referenced table.
  2. Ensure that the foreign key column(s) in the referencing table reference a unique column(s) in the referenced table.

By doing either of these, you'll establish a relationship between the tables that adheres to the constraints required by the database schema.

Was this helpful?

More Devops discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest DevOps Blogs

Guides, tips and career advice on DevOps from JanBask experts.