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 1lBh9Z-0007I7-0A for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:55:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBh9X-0002Y8-UZ for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:55:15 +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 1lBh9W-0002Xg-Si for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:55:15 +0000 Received: from mail-ot1-x32a.google.com ([2607:f8b0:4864:20::32a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lBh9T-0007Ww-Cg for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:55:13 +0000 Received: by mail-ot1-x32a.google.com with SMTP id l23so6581645otn.10 for ; Mon, 15 Feb 2021 08:55:11 -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=yo9MrwxX9j3AqJjH7g0iTRRsnCitZgLOzZ2DGdJGkic=; b=ghYy9Rfy4uSKlnJ4qNO1hBlHK7JyXzueIpwhgI891ErHLjvAP4FcPMRnenbRguiVNj gvQcgfDvqAujpfg17Uxo2clvjy1GBqjsLbx3UHW2HLb5eHrYmvMsTVEBkhrTy3Ccqch8 D1q9VbepBjpN6RnqRHIsqwZywknfL5SebF0kbb2vH1NP8nEl9F5JXSeubMQYuCGaS1gI 50DwlgEIzmp8wo6yL0yYsUdpBY30IWLgKVe9UB1tAzZXNchJ7xfu2PTHHuvozMCL5Z/y gxFiFl6+j7nIa+7ImBjd8wDgV4c3db+GJKn7OF2GLKx1fAAOqoLWn3p1v+yd5hR1+yrz sx8g== 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=yo9MrwxX9j3AqJjH7g0iTRRsnCitZgLOzZ2DGdJGkic=; b=T+zBaa3Wj+gNcISPz7NXNIe3c2nBOxxp0qxb4AdQMKNKF90kq/0z5Kbk5kRjE0i5KR mkbrCdllbiZrvX5B5LG+XskH0aJ0W4BVCl4VwVYDv9ySQnq1mAWR+SoPnJnKo217Abrc OEqc2zb2GCqZ9y+JfuQmQ+YW9r1uGX1e4qI7fbb2fCPZDw9gadFloM3ezDiOBPyDZtmW yRwayNrhdPVyQqpocegkIsUBcCZUKItu4XNaWg/VaDTQD2DeAGXHgKH+nukPlQZYjSq6 OlVmYU7gwHEcDIK6TfLBcVDOcnZm+JacRGAvrUM4dXuqjalS/o2bqFkTr7ri4KGuEHbl xZIA== X-Gm-Message-State: AOAM532C3vNytqX4iaeNLpWi2tGdN7EHEkgY4pvmcTULj9XZo/MCg05F 14PCpky+jsBO92clZrJUUNMEE401HutPrg== X-Google-Smtp-Source: ABdhPJzUgwDlhv5ekcUU3iFIk3J3ySwVudLtPfukjBygdBN4ZgSJabVrDJ/+2qHUwrdo6AdU0C7AWg== X-Received: by 2002:a05:6830:1651:: with SMTP id h17mr12850556otr.363.1613408110247; Mon, 15 Feb 2021 08:55:10 -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 c10sm428059otu.78.2021.02.15.08.55.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Feb 2021 08:55:09 -0800 (PST) Subject: Re: ADD FOREIGN KEY fails, but the records exist To: Adrian Klaver , "pgsql-generallists.postgresql.org" References: <37d7ad6b-3da6-0778-47fe-e6256babb4ad@aklaver.com> <22cd11dc-b6de-9056-fbed-1c53065fd1e0@aklaver.com> From: Ron Message-ID: Date: Mon, 15 Feb 2021 10:55:09 -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: <22cd11dc-b6de-9056-fbed-1c53065fd1e0@aklaver.com> Content-Type: multipart/alternative; boundary="------------22B8AE8CA325ECC18EBA2322" 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. --------------22B8AE8CA325ECC18EBA2322 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2/15/21 10:27 AM, Adrian Klaver wrote: > On 2/15/21 8:23 AM, Ron wrote: >> On 2/15/21 10:17 AM, Adrian Klaver wrote: >>> On 2/15/21 8:12 AM, Ron wrote: >>>> Postgresql 12.5 >>>> > >>> 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.) > > Yes but amended_response_id is referencing employer_response_id. So do you > have a record that matches: > > employer_response_id   part_date > > 103309154              2021-01-06 00:00:00 The time portions of the part_date fields don't match... 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, amended_response_id, part_date from strans.employer_response where amended_response_id = 103309154; employer_response_id | amended_response_id | part_date ----------------------+---------------------+---------------------             103309156 | *103309154 *| 2021-01-06*00:00:00* (1 row) sides=> sides=> sides=> select employer_response_id, amended_response_id, part_date from strans.employer_response where employer_response_id = 103309154; employer_response_id | amended_response_id | part_date ----------------------+---------------------+--------------------- *103309154* |                     | 2021-01-06 *15:14:03* (1 row) -- Angular momentum makes the world go 'round. --------------22B8AE8CA325ECC18EBA2322 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

On 2/15/21 10:27 AM, Adrian Klaver wrote:
On 2/15/21 8:23 AM, Ron wrote:
On 2/15/21 10:17 AM, Adrian Klaver wrote:
On 2/15/21 8:12 AM, Ron wrote:
Postgresql 12.5


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

Yes but amended_response_id is referencing employer_response_id. So do you have a record that matches:

employer_response_id   part_date

103309154              2021-01-06 00:00:00

The time portions of the part_date fields don't match...

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, amended_response_id, part_date
from strans.employer_response
where amended_response_id = 103309154;
employer_response_id | amended_response_id |      part_date    
----------------------+---------------------+---------------------
            103309156 |           103309154 | 2021-01-06 00:00:00
(1 row)

sides=>
sides=>
sides=> select employer_response_id, amended_response_id, part_date
from strans.employer_response
where employer_response_id = 103309154;
employer_response_id | amended_response_id |      part_date    
----------------------+---------------------+---------------------
            103309154 |                     | 2021-01-06 15:14:03
(1 row)


--
Angular momentum makes the world go 'round.
--------------22B8AE8CA325ECC18EBA2322--