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 1lBgUG-0005Q0-Nh for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:12:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBgUF-0001Pv-FV for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:12:35 +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 1lBgUF-0001Ph-5L for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:12:35 +0000 Received: from mail-oo1-xc35.google.com ([2607:f8b0:4864:20::c35]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lBgUC-0007Bu-PJ for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:12:34 +0000 Received: by mail-oo1-xc35.google.com with SMTP id s23so1636277oot.12 for ; Mon, 15 Feb 2021 08:12:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=hticNmcemlW7kh+02Fsuyzq14Kv/E69J/V7nXqZgsdw=; b=DSfKb4B0JPNDmgyf0eOAgzmYRlmHWnNTpZMKEVM5vYBc4bnQQomPu8tlOlZqbLSORI /rQ8UAfhlyawtJSDhLqV8pu5afP2biR0A70wkp82mKWoEc7zIbsczAuXHO45xRl+sc4E +jOU37kvQ4k5nYX2hThqs/93S3Gihlq1Adt3CiiVco5pems9X1SEN/Sq0DX+wKHwhs52 wUkjMbRKN6EKe5de1IecevanPs861csWXG0PHPGuwUzjFh/0pECcfXQxqYRSh1PFuXYM ltQSAmDTOm4ZkB7WkHdGYim3Exfv/oG/OUwkl6iq7EtBzqSwE81UkTP8jr/srMb5QnFA nfRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=hticNmcemlW7kh+02Fsuyzq14Kv/E69J/V7nXqZgsdw=; b=KotxozyTS2uqpC+TQI+G0SQT5EjPdc2B6eJC/f1EzVBK5FBzmEaJW/oE7a2LntWQxP YBZSpReDadZgiIhGJ/7mAhbi9guBDG5lKOwGxXiJelxFyOqi8KaNjWmOn889ik4D5DGv g+B0Tv1250pPfsn20ANMpeJ+1Jhb2ktt7FmdJXF7PBsrGv9istKr1Fwnp1RPOGe/tAEi 9zU0YBazVS1HSm1cU53C9w+KNBjZ96a8usk7vkCkG+LblIHdm5Mn/bp9oaXQOpey6c91 5zeO58YRurqX9nhtShsAyvQ5WSew+RhST2yilATv6Ggd6uJ5PmeoJ4W5+WFsjsGvAUfd P+/Q== X-Gm-Message-State: AOAM5325Ba649gKvzcTm8kIXHSx4lVHpLt9Os2xwssTpBNnc6clQSpW6 Ip+E4j2wARL44qMDKQo8+mNYAlNLYZVdzQ== X-Google-Smtp-Source: ABdhPJxEx5gpEcfWokFgr6IBKbT2JeatTUGHvlbtDiXDL9TN3l9CLqKisq9A+plnF6lnytTIUerDVA== X-Received: by 2002:a4a:db78:: with SMTP id o24mr11333339ood.69.1613405551595; Mon, 15 Feb 2021 08:12: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 v9sm3366360oon.11.2021.02.15.08.12.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Feb 2021 08:12:31 -0800 (PST) To: "pgsql-generallists.postgresql.org" From: Ron Subject: ADD FOREIGN KEY fails, but the records exist Message-ID: Date: Mon, 15 Feb 2021 10:12:30 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.