Re: Foreign key error...

From: Oliver Fromme <olli(at)lurza(dot)secnetix(dot)de>
To: Derrick(at)grifflink(dot)com (Derrick Betts)
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Foreign key error...
Date: 2003-10-10 18:16:28
Message-ID: 200310101816.h9AIGSWP082823@lurza.secnetix.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


Derrick Betts wrote:
> [...]
> The command I attempted to execute was:
> alter table casenotes
> add constraint fk_casenotes foreign key (caseid) references
> public.clientinfo (caseid) on update cascade on delete cascade;
>
> The error returned was:
> ERROR: fk_casenotes referential integrity violation - key referenced from
> casenotes not found in clientinfo
>
> Why will it work on some tables and not others? The "failing" table was
> created in the same manner as the others.

The "failing" table probably already contains one or more
rows with caseid values which are not in the clientinfo
table. You cannot add a constraint if the table already
violates it. This command will tell you the rows which
need to be fixed or deleted:

select * from casenotes where caseid not in
(select caseid from clientinfo);

Regards
Oliver

--
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

"File names are infinite in length, where infinity is set to 255 characters."
-- Peter Collinson, "The Unix File System"

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Psybar Phreak 2003-10-11 21:52:59 previous & next buttons
Previous Message Josh Berkus 2003-10-10 17:29:00 Re: output parameters in functions?