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 1ikClI-0006BB-BA for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 19:56:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikClG-0000sO-UO for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 19:56:02 +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 1ikClE-0000pa-UW for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 19:56:02 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ikCl9-00061b-HC for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 19:56:00 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id E5B9421CBA; Wed, 25 Dec 2019 14:55:52 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 25 Dec 2019 14:55:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=r 1MUq+yBSWIAczTXnBg9kls8wqGttv3VLg41ROCwN3w=; b=yiCVzHt40ynnPT/iR 2N4E3khHJH6CouU8BPYSbS2D+9lBL0TTGaWt55e2FzYu1Cp5lJA8y21ahvUQ9ud6 AUpcpuES08X+mrqIboMHcgRXL9IRJdfBCt4g530TTkushpBnDwG89s35WkeEGckL CiK533rkDprb8QdLcP1sNxRdXX09eXFa8FQ5rsn+xehY8DzWyeSZ4Uq5cbfHIREo jeulEmOort2ArAEOHb5dNI3tk4j6xL3EBYnSE7cPQwyDaW+gDrKban7XLr3NtbTM sO58EXPJkSDFDMmofjA8OLyXU4t01vJxFBNtglzGnsxBFqZAvtGSWT7V1yaY2pee 41+Qw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=r1MUq+yBSWIAczTXnBg9kls8wqGttv3VLg41ROCwN 3w=; b=OFrPagmitckja4b1omuhBI+F5GOZGCXvRhkiQL6AlJ/UCokuSESi1K0HD j+wZSUYIibe5ZoOS2tE8AkpnTO4aApJMqRmEG50RiNgur2Ybm36INvcp9ySMxwHg tyJi8BjF3/3TRcZhgem8FyJSfgCdw5CNJXty0Md4M4Z1zkxtBDn/cNt3pBpG0ZqU FxpM+AY2M3n3yKe0kZN/REpIG8DQUlRDazIDD767VUGpSZCvT1Pl02iDhYdiay9S 4lVLVJ/RjKcDa2zmjwlrh243ODgo+a7gunI3J+k4xxa52tFRcADrecgrhOh7qGix NIln7i9PjD43WFrB+aWi8MNeBzJxg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvddvgedgudefudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ejredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjedurddvuddvrdduvdelrd duleefnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegr khhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 27F99306098C; Wed, 25 Dec 2019 14:55:52 -0500 (EST) Subject: Re: Syntax question about returning value from an insert To: stan Cc: pgsql-general@lists.postgresql.org References: <20191225152638.GA19508@panix.com> <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.com> <20191225190832.GA21915@panix.com> From: Adrian Klaver Message-ID: <20e34cf2-0afb-24d0-4d5c-e3fa72f7f726@aklaver.com> Date: Wed, 25 Dec 2019 11:55:51 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20191225190832.GA21915@panix.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/25/19 11:08 AM, stan wrote: > > 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 >>> >>> 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 inserted into >>> t2, I an to go ahead and insert it. >> >> I'm with Pavel in not understanding what you want to do. This prevents any >> clear discussion on what to do below. To help: >> >> 1) Schema of t1 and t2. >> >> 2) Default for what column? >> >> 3) What is the key? >> > > First of all, thanks to both of you for your fast response . > > 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 am > creating a function that will query for the default category, like > > SELECT cost_category_key from t2 where type = '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 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 called On > insert to t1, and checks to see if the value supplied for this key is in > t2. > > Make more sense? No. It looks like you are trying to do a backwards FK. I would say your 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 above you have to know what the the type/key is before you INSERT into t1 anyway. -- Adrian Klaver adrian.klaver@aklaver.com