pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: ADD FOREIGN KEY fails, but the records exist
Date: Mon, 15 Feb 2021 10:12:30 -0600
Message-ID: <e8a458ad-2f92-6f8d-c485-8f72e3d4f72a@gmail.com> (raw)

Postgresql 12.5

It's a self-referential FK on a single (but partitioned) table.  The ALTER 
TABLE command fails, but I queried it, and the record that it fails on 
exists.  I modified the original INITIALLY IMMEDIATE clause to INITIALLY 
DEFERRED but that did not help.

What am I doing wrong?

(We're migrating from Oracle to PostgreSQL, and this is working in Oracle.)

sides=> ALTER TABLE employer_response
     ADD CONSTRAINT amended_response_fk FOREIGN KEY (amended_response_id, 
part_date)
         REFERENCES employer_response(employer_response_id, part_date)
     ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED ;
ERROR:  insert or update on table "employer_response_p2021_01" violates 
foreign key constraint "amended_response_fk"
DETAIL:  Key (amended_response_id, part_date)=(103309154, 2021-01-06 
00:00:00) is not present in table "employer_response".
sides=>
sides=> select employer_response_id, part_date
sides-> from strans.employer_response
sides-> where amended_response_id = 103309154;
employer_response_id |      part_date
----------------------+---------------------
             103309156 | 2021-01-06 00:00:00
(1 row)


sides=> select employer_response_id, part_date
from strans.employer_response_p2021_01
where amended_response_id = 103309154;
employer_response_id |      part_date
----------------------+---------------------
             103309156 | 2021-01-06 00:00:00
(1 row)


-- 
Angular momentum makes the world go 'round.





view thread (10+ messages)  latest in thread

Message-ID: <e8a458ad-2f92-6f8d-c485-8f72e3d4f72a@gmail.com>
Permalink:  ../e8a458ad-2f92-6f8d-c485-8f72e3d4f72a@gmail.com/
Also on:    postgresql.org/message-id/e8a458ad-2f92-6f8d-c485-8f72e3d4f72a@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com
  Subject: Re: ADD FOREIGN KEY fails, but the records exist
  In-Reply-To: <e8a458ad-2f92-6f8d-c485-8f72e3d4f72a@gmail.com>
  References: <e8a458ad-2f92-6f8d-c485-8f72e3d4f72a@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox