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 1lBgsK-0006Vt-A0 for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:37:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lBgsJ-0006Ct-7Z for pgsql-general@arkaria.postgresql.org; Mon, 15 Feb 2021 16:37:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lBgsI-0006Cd-W1 for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:37:26 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lBgsG-0007Xk-NO for pgsql-general@lists.postgresql.org; Mon, 15 Feb 2021 16:37:26 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 11FGbLgC2254789; Mon, 15 Feb 2021 11:37:21 -0500 From: Tom Lane To: Ron cc: "pgsql-generallists.postgresql.org" Subject: Re: ADD FOREIGN KEY fails, but the records exist In-reply-to: References: Comments: In-reply-to Ron message dated "Mon, 15 Feb 2021 10:12:30 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2254787.1613407041.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Mon, 15 Feb 2021 11:37:21 -0500 Message-ID: <2254788.1613407041@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ron writes: > 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? As Adrian noted, the queries you showed don't actually prove that the required employer_response_id exists in the table. However, if the identical data worked in Oracle then it should work in PG too, so for the moment I'll assume that that was a thinko and the FK should be valid. In that case I'd go looking for "invisible" reasons for the keys not to match. You did not show the column data types, but if the response ids are strings not numbers then I'd be wondering about extra spaces and such. Perhaps Oracle is more forgiving of such things than PG is. regards, tom lane