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 1lBgZY-0005dk-TM for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:18:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBgZX-0000eS-PX for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:18:03 +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 1lBgZW-0000b9-Q2 for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:18:03 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lBgZT-0007EM-GW for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:18:01 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id C4D9DFB2; Mon, 15 Feb 2021 11:17:57 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 15 Feb 2021 11:17:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=J wO3210SHLskfDSKr0Jlj/RTNwZtKGODt40PGkOGdw4=; b=ylVgJ+5BUrGNeqZaA KYR0ehiJi6j8dTwefq3B/wAJMHScueP5SWti1vtx4JirROT6lizDDJ2Cu32vi5xf 7L56djU141xPExjdmTO+V9vzieAoWM6rexmW2WgomnR4YesQut1BasciyOUjBIRs Wt0ygXbbLefO81kzn8nIBf8UlL5YIfgnUSHHQV8X9NFkwiQ8n0ps3RPaM5q1mqU6 KGJYaEGTEWizI3FXGlXYAQLE5KJjlwZuCF43GcwF9Lu7OOJNMZOsxH3dCaLrSph8 iDWT8oswrvmpUqidYS6i63XUOxaEu5LS6zybdni9NtVzAjRZaYNR8rwN5VbH5aDf VoZsQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=JwO3210SHLskfDSKr0Jlj/RTNwZtKGODt40PGkOGd w4=; b=PM5NBjbbNtMtbVWDaOhC+/XD1wDIiI0Wu1k0UdrRG642mUDmxTxFaJ98x uSBlIXDtfWdCIFj2iE2u5PzyJC6C7qBjBfOlJ2n5+nbtv+tdXvWGzZSAy5DOpuIy BZssSWOvGDSRYk/wqV8ELA4B05EgK6dA63IKWcZgKyDVgU2RCFOYh2Xux15ikMbk Z9giOcXpVbiwHMNkU7c0PCloxsxvVKtwu9M8WJFLScIfxCxoHMVLeE7zs0Cp3tdD GSjFb1VkuRZfnc+Tl6QEQAxXcyrd5vlUGlJRPKHCM72DA2Km9pVtFEL+2WnlR0YL o6pfdfQDJzceSWHZ14mm4yYFJu2Hg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrieekgdekfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepveevfeevfeeugeffkeekhfegieeufffgteeujedvgffh ueejjefhkeejleegtdegnecukfhppeeljedruddufedrvddujedrvdejnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgr vhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.4] (97-113-217-27.tukw.qwest.net [97.113.217.27]) by mail.messagingengine.com (Postfix) with ESMTPA id BFEC11080059; Mon, 15 Feb 2021 11:17:56 -0500 (EST) Subject: Re: ADD FOREIGN KEY fails, but the records exist To: Ron , "pgsql-generallists.postgresql.org" References: From: Adrian Klaver Message-ID: <37d7ad6b-3da6-0778-47fe-e6256babb4ad@aklaver.com> Date: Mon, 15 Feb 2021 08:17:55 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 > > > 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) > > -- Adrian Klaver adrian.klaver@aklaver.com