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 1hVx8Q-0007Yn-1b for pgsql-general@arkaria.postgresql.org; Wed, 29 May 2019 11:52:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hVx8N-0004eS-TQ for pgsql-general@arkaria.postgresql.org; Wed, 29 May 2019 11:52:43 +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 1hVx8N-0004eK-GE for pgsql-general@lists.postgresql.org; Wed, 29 May 2019 11:52:43 +0000 Received: from n3.nabble.com ([162.255.23.22]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hVx8K-0006m2-8g for pgsql-general@postgresql.org; Wed, 29 May 2019 11:52:41 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id ECB8114611ADE for ; Wed, 29 May 2019 04:52:38 -0700 (MST) Date: Wed, 29 May 2019 04:52:38 -0700 (MST) From: PegoraroF10 To: pgsql-general@postgresql.org Message-ID: <1559130758966-0.post@n3.nabble.com> Subject: with and trigger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk We like to use With to insert, update and return some value to user. But so= me informations of those related tables are not available on that time, is tha= t a bug ? with=20 Master(Master_ID) as (insert into Master(Customer_ID, Field2) values(1, 'BlaBla') returning Master_ID), Detail as (insert into Detail(Master_ID, Product_ID, ProductValue) select Master_ID, 5, 50 from Master) select Master_ID from Master; This code works but not as expected because we have a trigger which does no= t see that data yet. Suppose a trigger on Detail which needs to find any info from a table which was inserted on this With. That info is not available, like ... create function DetailOfDetail() returns trigger() as -- this trigger function is before insert on Detail begin new.Discount =3D (select discount from Customer inner join Master using(Customer_ID) where Master_ID =3D new.Master_ID) end; This trigger will not work because Master record was not inserted yet. If change it to a DO it would work but we would like that result, so ... This trigger is obviously an example, our tables have more complex structures but here we want only to understand the way postgres works or if it=C2=B4s not working properly. -- Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.ht= ml