foreign key restrictions

From: rafal(at)zorro(dot)isa-geek(dot)com
To: "pgsql general" <pgsql-general(at)postgresql(dot)org>
Subject: foreign key restrictions
Date: 2008-08-10 08:15:51
Message-ID: 64cc57edd02dabd82e3f95268aee1a67.squirrel@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi All,

on numerous times I had fell onto postgress complaining, that I try to
create foreign key, pointing to a set not embraced within a unique key
constraint.

Here is the case:

CREATE TABLE one (id int not null unique, info text);
CREATE TABLE two (id int not null unique, ofone int references one(id),
info text);

now when I try to:

CREATE TABLE three(one int not null, two int, info text, foreign key (one,
two) references two (one, id));

I get the following error:
ERROR: there is no unique constraint matching given keys for referenced
table "two"

But.

Since table TWO has a unique constraint on column ID, the (ID, OFONE) pair
will also be unique, obviously.

Naturaly I can, and I do, add the requested constraint to the table TWO,
but to me it looks spurious - not providing any additional constraint
(which is already quearanteed by unique(ID), just a 'syntax glue'.

I must have missed something here. Can someone help me understand this?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Henry 2008-08-10 13:03:17 Re: [Pgpool-general] Trying to switch from PgPool1 to PgPool2
Previous Message Scott Marlowe 2008-08-09 20:54:49 Re: 100% CPU pg processes that don't die.