Re: Integrity violation when adding foreign key constraint

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Daniel Hartmeier <daniel(at)reichardt(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Integrity violation when adding foreign key constraint
Date: 2001-03-26 15:20:59
Message-ID: Pine.BSF.4.21.0103260718230.59290-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Unfortunately there is a bug in 7.0.x's alter table add constraint
that gets the column definitions wrong (I had misinterpreted the
ordering of arguments to the trigger). I think I sent the patch
to someone over the mailing list so it should be in the archives.

On Mon, 26 Mar 2001, Daniel Hartmeier wrote:

> I hope somebody can help me with a question. I have two tables
>
> CREATE TABLE sk (
> fnr INTEGER,
> knr SMALLINT,
> [...]
> CONSTRAINT sk_pk_fnr_knr PRIMARY KEY ( fnr, knr )
> );
>
> CREATE TABLE sg (
> fnr INTEGER,
> knr SMALLINT,
> gpc CHAR(1),
> [...]
> CONSTRAINT sg_pk_fnr_knr_gpc PRIMARY KEY ( fnr, knr, gpc )
> );
>
> loaded with data. When I try to add a foreign key constraint with
>
> ALTER TABLE sg ADD CONSTRAINT sg_fk_fnr_knr FOREIGN KEY ( fnr, knr )
> REFERENCES sk;
>
> I get the error message
>
> NOTICE: ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for
> FOREIGN KEY check(s)
> ERROR: <unnamed> referential integrity violation - key referenced from
> sg not found in sk
>
> But when I try to find the offending row(s) in sg with
>
> SELECT * FROM sg WHERE NOT EXISTS ( SELECT * FROM sk WHERE
> sk.fnr = sg.fnr AND sk.knr = sg.knr );
>
> I get nothing (0 rows).
>
> This is PostgreSQL 7.0.3 on BSD, and I ran vacuum analyze on both tables
> first.
>
> Am I doing something wrong, or what might be a reason for what looks
> like a contradiction to me? Foreign keys on multiple columns work like
> this, don't they?
>
> Kind regards,
> Daniel
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-03-26 15:27:22 Re: Hi
Previous Message fabrizio.ermini 2001-03-26 15:18:01 Re: Free PostgreSQL Database Hosting - Needs Beta Testers