Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZX9Nq-0004N3-2E for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Sep 2015 14:51:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZX9Np-0000sd-LD for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Sep 2015 14:51:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZWuwM-00029j-IF for pgsql-bugs@postgresql.org; Tue, 01 Sep 2015 23:26:10 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZWuwF-0008Ia-4V for pgsql-bugs@postgresql.org; Tue, 01 Sep 2015 23:26:09 +0000 MIME-Version: 1.0 Subject: BUG #13603: Foreign Key Constraint is invalid without any error To: pgsql-bugs@postgresql.org From: gavin@fusionbox.com Date: Tue, 01 Sep 2015 23:25:19 +0000 Message-ID: <20150901232519.1367.83187@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.2 (-) Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 13603 Logged by: Gavin Wahl Email address: gavin@fusionbox.com PostgreSQL version: 9.4.4 Operating system: Linux Description: I have a database with a foreign key constraint, with data in in that violates the constraint. Shouldn't that be impossible? The DEFERRABLE isn't related -- the transaction that added this data has already committed. The data came from pgloader so I'm not sure the exact query that was able to insert data violating the fk constraint. # \d products_specifications Table "public.products_specifications" Column | Type | Modifiers ------------+------------------------+---------------------------------------------------------------------- id | integer | not null default nextval('products_specifications_id_seq'::regclass) name | character varying(255) | not null product_id | integer | Indexes: "products_specifications_pkey" PRIMARY KEY, btree (id) "products_specifications_9bea82de" btree (product_id) Foreign-key constraints: "D5225bac506acc40e6f759b266a3b1c8" FOREIGN KEY (product_id) REFERENCES products_product(variant_ptr_id) DEFERRABLE INITIALLY DEFERRED # \d products_product Table "public.products_product" Column | Type | Modifiers --------------------+------------------------+----------- variant_ptr_id | integer | not null Indexes: "products_product_pkey" PRIMARY KEY, btree (variant_ptr_id) Referenced by: TABLE "products_specifications" CONSTRAINT "D5225bac506acc40e6f759b266a3b1c8" FOREIGN KEY (product_id) REFERENCES products_product(variant_ptr_id) DEFERRABLE INITIALLY DEFERRED # SELECT * FROM products_specifications WHERE product_id = 15; id | name | product_id ----+-------------+------------ 5 | 200 threads | 15 (1 row) # SELECT * FROM products_product WHERE variant_ptr_id = 15; variant_ptr_id --------------- (0 rows) Inserting new rows fails: # INSERT INTO products_specifications (name, product_id) VALUES ('foo', 15); ERROR: insert or update on table "products_specifications" violates foreign key constraint "D5225bac506acc40e6f759b266a3b1c8" DETAIL: Key (product_id)=(15) is not present in table "products_product". But updating the existing row succeeds: # UPDATE products_specifications SET product_id = 15 WHERE id = 5; UPDATE 1 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs