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 1ikDVj-000813-2a for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 20:44:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikDVh-0004jy-V3 for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 20:44:01 +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 1ikDVh-0004jn-JR for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 20:44:01 +0000 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ikDVb-0006LF-3r for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 20:44:01 +0000 Received: by mail-pg1-x535.google.com with SMTP id l24so11976859pgk.2 for ; Wed, 25 Dec 2019 12:43:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=content-transfer-encoding:from:mime-version:subject:date:message-id :references:cc:in-reply-to:to; bh=sdCz1oMtimEap9cT8ralqPNla5h4A+lwm3lLxVVseYM=; b=g1NIF6LvPkBDpNAdjVA4bOVkRAVckTajWocXeUoF6CBxy5QRFfIjk6Lrs4ulmaj+fm tZ4maCQM5XdgJOfa9DMSv8Qfb/NRIPrRoa7gSc2jzAxJHqB+ZTba0EIiM6wpyAR5qN+g ycjHlIMzM2jTdtC1xI/w25Nq2yd4+L5sUa/aRSTbdMsaSW+kqbXLXuH1njFSlbVzphLr iNuyH0Zqhqi5DAqWiMZnWbgR1SUVWDpiLOS7yeZWJcxuNYB/5HtGGMI/LP8NyguS1LM0 bHY0FIvcdaf884g1XBSajAV6OPvci7OO/ywLpa4la6NtygQ9PoUE6Gm08iYtB3kQ2Rje pEOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:date:message-id:references:cc:in-reply-to:to; bh=sdCz1oMtimEap9cT8ralqPNla5h4A+lwm3lLxVVseYM=; b=JlKRfwvGrmc+jT47Ih2lOpvOdFl1UC3n5skZWhfoCoLupSHQU/aQDioz+tSDd8+yYT 0KLQqa4Z+rG7WgixYqrmYWH6N2L0bnll6GCeFoog/ybSsDWAcE+oZPocltI8Hx45vaMk zZBm673LVDjGUf1X4nX3wSvstpP2zf8pE0WmZlzJzVfOdH/bAWpK/jzYuLY4nir9c0ME dCUdDEhBt5+HwzOJC7gkedlwZgBpNDv+ZiWK/5P4wi+2i9JDlXMy8qTpeLelJtj2tGzL RBJFIoySD/1PEQMGSMATgSwnAIai3yCKjblkxPkSKusFMq/byFwdUc47oXSHpRW/u7jZ uafA== X-Gm-Message-State: APjAAAXDZrosOvj3eCPaHOcGaZVbkzZtBZBkeaq76wVKAt+D56h2X52g fVX5/3PQ8ap9qQTtFD4+5lmD3Ur4zwcMoQ== X-Google-Smtp-Source: APXvYqxi/DiuLLhR4J2cEQ1KivLIHFjJ8jt1/CPlrLGdsMKV6QTNrUuTXx0R7pDlAHam5dDU6S8HZw== X-Received: by 2002:a62:e40e:: with SMTP id r14mr45054127pfh.115.1577306631554; Wed, 25 Dec 2019 12:43:51 -0800 (PST) Received: from [192.168.1.80] (d23-16-92-185.bchsia.telus.net. [23.16.92.185]) by smtp.gmail.com with ESMTPSA id k44sm7717054pjb.20.2019.12.25.12.43.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Dec 2019 12:43:51 -0800 (PST) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Rob Sargent Mime-Version: 1.0 (1.0) Subject: Re: Syntax question about returning value from an insert Date: Wed, 25 Dec 2019 12:43:50 -0800 Message-Id: References: <20e34cf2-0afb-24d0-4d5c-e3fa72f7f726@aklaver.com> Cc: stan , pgsql-general@lists.postgresql.org In-Reply-To: <20e34cf2-0afb-24d0-4d5c-e3fa72f7f726@aklaver.com> To: Adrian Klaver X-Mailer: iPhone Mail (17C54) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Dec 25, 2019, at 11:56 AM, Adrian Klaver < >> First of all, thanks to both of you for your fast response =7F. >> Let me clarify. >> I have a table that records will be inserted into. Several of the columns= >> in this table must be non NULL, and they are actually keys from other >> tables. Like >> Table t1 >> has a column like cost_category_key >> So if an INSERT to this table gets called with this column as a NULL, I a= m >> creating a function that will query for the default category, like >> SELECT cost_category_key from t2 where type =3D 'Misc' >> 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 t= o >> do is go ahead and insert this row. Once this is done, the correct defaul= t >> row will exist in T2, but I still need the (automatically assigned) key f= or >> this row to place in the NEW. structure for the function that is called O= n >> insert to t1, and checks to see if the value supplied for this key is in >> t2. >> Make more sense? >=20 > No. It looks like you are trying to do a backwards FK. I would say your li= fe would be a lot easier if you used FK's as intended e.g. have t2 be the pa= rent table and INSERT the correct type/key combination there first before yo= u INSERT into t1, as separate operations. As you script it out above you hav= e to know what the the type/key is before you INSERT into t1 anyway. >=20 >=20 If you know today what those defaults are then load them today. That allows a= standard FK from t1 to t2. Also streamlines adding new values (no code req= uired). Your current plan is at risk of typos causing new bogus defaults.=20= Are your multiple non-null columns each a separate domain referencing separa= te =E2=80=9Ct2=E2=80=9Ds? > --=20 > Adrian Klaver > adrian.klaver@aklaver.com >=20 >=20