Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmZ18-0000wP-On for pgsql-admin@arkaria.postgresql.org; Thu, 24 Jan 2019 07:01:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gmZ16-0006eu-3H for pgsql-admin@arkaria.postgresql.org; Thu, 24 Jan 2019 07:01:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmZ15-0006en-MI for pgsql-admin@lists.postgresql.org; Thu, 24 Jan 2019 07:01:35 +0000 Received: from mail-ot1-x32b.google.com ([2607:f8b0:4864:20::32b]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmZ12-0003Sa-8Y for pgsql-admin@lists.postgresql.org; Thu, 24 Jan 2019 07:01:34 +0000 Received: by mail-ot1-x32b.google.com with SMTP id v23so4338797otk.9 for ; Wed, 23 Jan 2019 23:01:31 -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=XsHNh+phfV7fNQYFjOINEA7AIvktHpOS23wZLgJh2B4=; b=rNu6VqT/cxJo4gtaKBvhdk710EN7jyCkhFhOwv8+uDsQNO2pUrA8j6+5RI54jmy4du x0BE0hCZDMmsJE7eavf/WGMNG5Mp6FTGgaNzVOa8v3ROMuOtqgIHSGDpt3hWZrDyz8JQ 2RXpzP4kLS2lbUyjLDempPLMIcTiIwUP7BQW49c8lgS20MihjviLROF4quySPwWEnphs kch3+G/CwwJQ3RS6iZl54VXGEkV+e0inVaA+3z9OLnnPvWcaifQvlitilV//RKOHeIqK nS2bDHIpNndS94AIFm+HZJfPJzieLzh0KA5JtKtFuk6FyymgQUtP48lG3MGDYabV83qa d1kg== 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=XsHNh+phfV7fNQYFjOINEA7AIvktHpOS23wZLgJh2B4=; b=hDEsH7yeGXmQy4lHIRBG4pazW3z/gGIIC/JJGkxGu9s5sXNsWwABb3/Wz0EE41yREe xGAxCVwhSXNjOr7PdbXDwSq3aerA4nvX7f8Vg3oVPGelxokm5XWGdwHO5+atDQFp33T0 KCxz0ySnx/0cu0qlouATIktvfCTGeYNOaLRUZansEVMSoYjD+gqMlxeZLuG57rdMLW6B s/9ijVxxC1pBWA5CEit13Uu3OL5AEfkKOSeMVKzsrWY9pPknUpQQ6wXLaAw7EJt3RPOh 3QDTGDEuga95xVa9DCvrk5+2wBIw6HYdv681cIgbFTON3La5UgqJnn/4R9hJOvunPRe8 9FCA== X-Gm-Message-State: AJcUukdACpW4T1kuyA9Bz+gTLvJppkUwUYYcUC2tpUhrlrBG5UiJA20Q sgqe1Yvq+Y4yEy7qfNEjEKWHqKAs X-Google-Smtp-Source: ALg8bN5WjHsEnxy0+SECzX3aSGeurN7zmEaOjFEOLZpFVi3zMFxpHp9Mgw/D445CfWQAfcF7oRLNnQ== X-Received: by 2002:a9d:480f:: with SMTP id c15mr3625438otf.265.1548313290152; Wed, 23 Jan 2019 23:01:30 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id u19sm10367121oia.31.2019.01.23.23.01.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Jan 2019 23:01:29 -0800 (PST) Subject: Re: constraint deferred but fails? To: pgsql-admin@lists.postgresql.org References: <7db1a61c-1f0e-d233-0756-b7eecf2f420d@shopzeus.com> From: Ron Message-ID: Date: Thu, 24 Jan 2019 01:01:27 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <7db1a61c-1f0e-d233-0756-b7eecf2f420d@shopzeus.com> Content-Type: multipart/alternative; boundary="------------4E19363D73B146D4DB322788" 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. --------------4E19363D73B146D4DB322788 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 1/24/19 12:35 AM, Nagy László Zsolt wrote: > Hello, > > I have deferred the only fk constraint to this table inside a > transaction, but I still cannot delete rows from it. > > How is that possible? > > > master=> ALTER TABLE doc.display_mode disable trigger user; > ALTER TABLE > master=> BEGIN; > BEGIN > master=> SET CONSTRAINTS doc.fk_book_display_mode_id DEFERRED; > SET CONSTRAINTS > master=> DELETE FROM doc.display_mode; > ERROR:  update or delete on table "display_mode" violates foreign key > constraint "fk_book_display_mode_id" on table "book" > DETAIL:  Key (id)=(1) is still referenced from table "book". > master=> Deferring a constraint doesn't mean "disappearing" the constraint; it just means... *deferring* it until later (i.e. commit time).  It's only purpose is to allow your code to delete from doc.display_mode before deleting from book .  But you *do* have to delete from book at some point in the transaction. -- Angular momentum makes the world go 'round. --------------4E19363D73B146D4DB322788 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 1/24/19 12:35 AM, Nagy László Zsolt wrote:
Hello,

I have deferred the only fk constraint to this table inside a
transaction, but I still cannot delete rows from it.

How is that possible?


master=> ALTER TABLE doc.display_mode disable trigger user;
ALTER TABLE
master=> BEGIN;
BEGIN
master=> SET CONSTRAINTS doc.fk_book_display_mode_id DEFERRED;
SET CONSTRAINTS
master=> DELETE FROM doc.display_mode;
ERROR:  update or delete on table "display_mode" violates foreign key
constraint "fk_book_display_mode_id" on table "book"
DETAIL:  Key (id)=(1) is still referenced from table "book".
master=>

Deferring a constraint doesn't mean "disappearing" the constraint; it just means... deferring it until later (i.e. commit time).  It's only purpose is to allow your code to delete from doc.display_mode before deleting from book .  But you do have to delete from book at some point in the transaction.

--
Angular momentum makes the world go 'round.
--------------4E19363D73B146D4DB322788--