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 1ikDSR-0007tB-6z for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 20:40: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 1ikDRR-0000fn-C9 for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 20:39:37 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1ikDRR-0000fg-29 for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 20:39:37 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ikDRO-0006JD-0o for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 20:39:36 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47jlLq2LzKz1JPV; Wed, 25 Dec 2019 15:39:31 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47jlLq0kPbzfYc; Wed, 25 Dec 2019 15:39:31 -0500 (EST) Date: Wed, 25 Dec 2019 15:39:30 -0500 From: stan To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: Syntax question about returning value from an insert Message-ID: <20191225203930.GA29046@panix.com> References: <20191225152638.GA19508@panix.com> <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.com> <20191225190832.GA21915@panix.com> <20e34cf2-0afb-24d0-4d5c-e3fa72f7f726@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <20e34cf2-0afb-24d0-4d5c-e3fa72f7f726@aklaver.com> X-Editor: gVim User-Agent: Mutt/1.12.1 (2019-06-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Dec 25, 2019 at 11:55:51AM -0800, Adrian Klaver wrote: > On 12/25/19 11:08 AM, stan wrote: > >=20 > > On Wed, Dec 25, 2019 at 08:28:45AM -0800, Adrian Klaver wrote: > > > On 12/25/19 7:26 AM, stan wrote: > > > > I am writing a trigger/function to make certain a default item, and= its key > > > > exist when an insert is called. EG > > > >=20 > > > > The trigger gets called on insert to T1 If column c1 is NULL in the= NEW > > > > structure, I need to check table t2 to get the key associated with = the > > > > default for this column. However, if the default is not yet inserte= d into > > > > t2, I an to go ahead and insert it. > > >=20 > > > I'm with Pavel in not understanding what you want to do. This prevent= s any > > > clear discussion on what to do below. To help: > > >=20 > > > 1) Schema of t1 and t2. > > >=20 > > > 2) Default for what column? > > >=20 > > > 3) What is the key? > > >=20 > >=20 > > First of all, thanks to both of you for your fast response =7F. > >=20 > > Let me clarify. > >=20 > > I have a table that records will be inserted into. Several of the colum= ns > > in this table must be non NULL, and they are actually keys from other > > tables. Like > >=20 > > Table t1 > > has a column like cost_category_key > >=20 > > So if an INSERT to this table gets called with this column as a NULL, I= am > > creating a function that will query for the default category, like > >=20 > > SELECT cost_category_key from t2 where type =3D 'Misc' > >=20 > > Now suppose that the default category has not yet been inserted in T2. I > > can easily detect this as the SELECT will return a NULL. So what I want= to > > do is go ahead and insert this row. Once this is done, the correct defa= ult > > row will exist in T2, but I still need the (automatically assigned) key= for > > this row to place in the NEW. structure for the function that is called= On > > insert to t1, and checks to see if the value supplied for this key is in > > t2. > >=20 > > Make more sense? >=20 > No. It looks like you are trying to do a backwards FK. I would say your l= ife > would be a lot easier if you used FK's as intended e.g. have t2 be the > parent table and INSERT the correct type/key combination there first befo= re > you INSERT into t1, as separate operations. As you script it out above you > have to know what the the type/key is before you INSERT into t1 anyway. No, the key is auto generated using a sequence on the INSERT into t2 Which is where this gets interesting. If I try to select for it and a NULL is returned, then I KNOW I need to insert it. As a consequence of this, the new key is generated. I need to populate (replace the NULL) that comes in the NEW. record with the newly generated key. The original article says that I can get the results of an insert, to use. That's what I have in mind. Other approaches welcome. --=20 "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin