Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lBgiC-0005yu-B8 for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:27:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBgiB-000283-AD for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:26:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lBges-0002iL-GF for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:23:34 +0000 Received: from mail-oi1-x234.google.com ([2607:f8b0:4864:20::234]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lBgep-0007H1-Ua for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:23:33 +0000 Received: by mail-oi1-x234.google.com with SMTP id l19so8264846oih.6 for ; Mon, 15 Feb 2021 08:23:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=OulRtPk71i73TtyaEZ+NJjoiE3pweOufNLvb9w5YDDA=; b=TwEq//7c+W1i3Q1HuS/bIfFJ26M2oS+SpxhaqnKo3wr/llTbGHES0cyhqWdvINT5cC Kpuh1Ori9pyrp799SOLlFrhSLiRARuddqDnmplgBus0a2fcnO+uqZSpcQ7tdb49ldrqs 2nNvQPs40Nin4UYWKUnGBHKDJgvg5Tzs3+TJxxs0NF2sVnNKpJiAZkfH8GY8BOPis0hh NLxagRA1mM1HVThchHKszaEyYpDdTERapYjEAr3bHtQNquwGjw6GmbZzo3M8Oz6u5eX9 liYQElCpLRY5pmoW6svdJ7SC9Y31m5fZyVBlNfQ7MXSHkxxJ4EmcboXIrVl/SztyYyY6 uvdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=OulRtPk71i73TtyaEZ+NJjoiE3pweOufNLvb9w5YDDA=; b=odQNNoZ+aezBWOYV9VS7woiFFYRgBfbBNbpahoDhGOZjGGw3MELPx7i1rRlcp/HRSv B9ZJXE+FD2QI7txu/G4hHKZAeTHMxfJcFrYc/G8mho97swIVU7Ybc53EL67MZuQYGV7d oUZP2GCZM+fFs8+R6frxb98Z3gxUggqkC8ZJbYj1TuwUWPKkIX7yPFrWMAsDZv+bEbfr rWovb0jc+lDi70dZM0wo2QUdEC3EKN9lT1utPCbVQiZG0w7conHsW806KIWoq+12JdmG kPtk6s+AveOqEzYM8hb0QCmtz0DBpsbVmwoeqKjR0W7aeLwbgCJuAl0ptAXe1moSvxsv dLAg== X-Gm-Message-State: AOAM5331DW0DC7naweq2UPtHsTNdEUU1KPgda4cxcRvqmm75Y5MJSiie ZpIcv8qrjTJ1hGVJW1uFEhvQE4jWlJJU8A== X-Google-Smtp-Source: ABdhPJzyCoDwjIqr8Ytcf6IPeRNwQQgDTY+p2/d3QAR9u04gi7LkqT8V8U7i03El+fiPsFTksr5urw== X-Received: by 2002:a05:6808:1390:: with SMTP id c16mr8462785oiw.27.1613406211138; Mon, 15 Feb 2021 08:23:31 -0800 (PST) Received: from [192.168.88.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id 3sm3965046oid.27.2021.02.15.08.23.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Feb 2021 08:23:30 -0800 (PST) Subject: Re: ADD FOREIGN KEY fails, but the records exist To: "pgsql-generallists.postgresql.org" References: <37d7ad6b-3da6-0778-47fe-e6256babb4ad@aklaver.com> From: Ron Message-ID: Date: Mon, 15 Feb 2021 10:23:29 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <37d7ad6b-3da6-0778-47fe-e6256babb4ad@aklaver.com> Content-Type: multipart/alternative; boundary="------------FEBCB735A0D6F5DF22D1C548" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------FEBCB735A0D6F5DF22D1C548 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2/15/21 10:17 AM, Adrian Klaver wrote: > On 2/15/21 8:12 AM, Ron wrote: >> 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) > > The error: > > DETAIL:  Key (amended_response_id, part_date)=(103309154, 2021-01-06 > 00:00:00) is not present in table "employer_response" > > is pointing at 103309154 for amended_response_id = employer_response_id. > You are showing an employer_response_id of 103309156 But my query's WHERE clause specifies "amended_response_id = 103309154;"  (I've highlighted it, if you have a GUI MUA.) > >> >> >> 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. --------------FEBCB735A0D6F5DF22D1C548 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 2/15/21 10:17 AM, Adrian Klaver wrote:
On 2/15/21 8:12 AM, Ron wrote:
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)

The error:

DETAIL:  Key (amended_response_id, part_date)=(103309154, 2021-01-06 00:00:00) is not present in table "employer_response"

is pointing at 103309154 for amended_response_id = employer_response_id. You are showing an employer_response_id of 103309156

But my query's WHERE clause specifies "amended_response_id = 103309154;"  (I've highlighted it, if you have a GUI MUA.)




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.
--------------FEBCB735A0D6F5DF22D1C548--