Re: Foreign key problem - Mailing list pgsql-sql

From Itai Zukerman
Subject Re: Foreign key problem
Date
Msg-id 878zig1wme.fsf@matt.w80.math-hat.com
Whole thread Raw
In response to Foreign key problem  (Andreas Tille <tillea@rki.de>)
Responses Re: Foreign key problem
List pgsql-sql
Je Mon, 25 Jun 2001 09:34:01 +0200 (CEST),
Andreas Tille <tillea@rki.de> scribis:

> CREATE TABLE ResKulturDetail
> (
>     IdLabNr   int,
>     IdIndex   smallint
> );
> 
> CREATE TABLE ResKulturDetailDay
> (
>     IdLabNr int,
>     IdIndex smallint
> );
> 
> CREATE INDEX IX_IdLabNr_KulturDetail ON ResKulturDetail(IdLabNr) ;
> 
> ALTER TABLE ResKulturDetailDay ADD CONSTRAINT FK_ResKulturDetailDay
>   FOREIGN KEY (IdLabNr,IdIndex)
>   REFERENCES ResKulturDetail (IdLabNr,IdIndex) ;

> Can anybody explain, why the foreign key constraint fails?

According to the documentation for CREATE TABLE:
 In addition, the referenced columns are supposed to be the columns of a UNIQUE constraint in the referenced table,
howeverPostgres does not enforce this.
 

Well, it looks like PostgreSQL *does* enforce it.  Try it with:
 CREATE UNIQUE INDEX IX_IdLabNr_KulturDetail ON ResKulturDetail(IdLabNr, IdIndex) ;

I'm not sure why this restriction is necessary...

-- 
Itai Zukerman  <http://www.math-hat.com/~zukerman/>


pgsql-sql by date:

Previous
From: Christof Glaser
Date:
Subject: Re: Foreign key problem
Next
From: Luis Sousa
Date:
Subject: Problems using a rule with the WHERE clause