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 1ikFn6-0005aw-6e for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 23:10:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikFn3-0002yY-7o for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 23:10:05 +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 1ikFn2-0002yP-H6 for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 23:10:04 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ikFmv-00025Z-Ei for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 23:10:02 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47jphN1hs9z1Mf9; Wed, 25 Dec 2019 18:09:56 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47jphN095lzfYc; Wed, 25 Dec 2019 18:09:55 -0500 (EST) Date: Wed, 25 Dec 2019 18:09:55 -0500 From: stan To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: Syntax question about returning value from an insert Message-ID: <20191225230955.GB18451@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> <20191225203930.GA29046@panix.com> <29d5c039-9127-aa82-fa8f-1643ee883699@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <29d5c039-9127-aa82-fa8f-1643ee883699@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 02:34:43PM -0800, Adrian Klaver wrote: > On 12/25/19 12:39 PM, stan wrote: > >=20 > > 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 w= ith the > > > > > > default for this column. However, if the default is not yet ins= erted into > > > > > > t2, I an to go ahead and insert it. > > > > >=20 > > > > > I'm with Pavel in not understanding what you want to do. This pre= vents 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 c= olumns > > > > in this table must be non NULL, and they are actually keys from oth= er > > > > 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 NUL= L, 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 = default > > > > 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 ca= lled 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 yo= ur life > > > 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 = before > > > you INSERT into t1, as separate operations. As you script it out abov= e you > > > have to know what the the type/key is before you INSERT into t1 anywa= y. > >=20 > > No, the key is auto generated using a sequence on the INSERT into t2 > >=20 > > Which is where this gets interesting. If I try to select for it and a N= ULL > > 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 co= mes > > in the NEW. record with the newly generated key. > >=20 > > The original article says that I can get the results of an insert, to u= se. >=20 > You can given the correct conditions. Since you did not include the syntax > error in the original post it is is difficult to say what the problem is = and > I'm not going to just throwing out guesses. Sorry, I was trying to just get across what I am trying to do without writing in large unrelated sections. >=20 > >=20 > > That's what I have in mind. > >=20 > > Other approaches welcome. >=20 > Other approaches have been suggested, e.g. use a traditional FK > relationship. The big unknown in you present system is what: >=20 > SELECT cost_category_key from t2 where type =3D 'Misc' >=20 > will return. The implication is that it maybe more then one value(key) in > that case, which key would you use? There is more that that. There is a project number, so the actuall key represents the combination of project number, and cost category, Thire is a constraint on T2 that assures that these combinations will be unique.=20 When the 1st record for a project, that gets charged to the misc. cost category is entered, I need to assure that combination gets inserted into T2 > If not and cost_category_key: type is one to one then why have both? > Just make one unique(or PK) and the parent for the cost_category_key in t= 1. --=20 "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin