Re: unique constraints

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Kenneth Gonsalves <lawgon(at)thenilgiris(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: unique constraints
Date: 2004-01-07 17:31:47
Message-ID: 20040107103147.A11576@quality.qadas.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Jan 07, 2004 at 07:47:32PM +0530, Kenneth Gonsalves wrote:
> in 7.4 i'm getting an error message - UNIQUE constraint matching keys for
> table 'xxx' not found - any clues?

It would be helpful to see the *exact* wording of the error message and
the SQL statements that can be used to reproduce the error.

I'd guess that you tried to create a foreign key constraint whose
referenced column(s) didn't have a UNIQUE constraint:

=> CREATE TABLE foo (foo_id INTEGER NOT NULL);
CREATE TABLE
=> CREATE TABLE bar (foo_id INTEGER NOT NULL REFERENCES foo (foo_id));
ERROR: there is no unique constraint matching given keys for referenced table "foo"

The referenced column(s) should either be a PRIMARY KEY or have a UNIQUE
constraint:

=> CREATE TABLE foo (foo_id INTEGER PRIMARY KEY);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey" for table "foo"
CREATE TABLE
=> CREATE TABLE bar (foo_id INTEGER NOT NULL REFERENCES foo (foo_id));
CREATE TABLE

See the "Foreign Keys" documentation for more info:

http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-FK

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2004-01-07 18:25:46 Re: Notation of index
Previous Message Andreas 2004-01-07 17:09:04 Notation of index