pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: How to check if a field exists in NEW in trigger
Date: Mon, 5 Aug 2019 07:55:29 +0200
Message-ID: <37107788-df16-ca5f-3193-6176fe40f282@gmx.net> (raw)
In-Reply-To: <0cc87310-590f-740e-977d-bcdde57eef1b@lucee.org>
References: <0cc87310-590f-740e-977d-bcdde57eef1b@lucee.org>

Igal @ Lucee.org schrieb am 05.08.2019 um 00:52:
> I have the following statement in a trigger:
> 
>     new.email = lower(new.email);
> 
> When I try to update a record without setting the email column however, I get an error:
> 
> SQL Error [42703]: ERROR: record "new" has no field "email"
>   Where: SQL statement "SELECT lower(new.email)"
> PL/pgSQL function on_record_modified() line 26 at assignment
> 
> I have seen some hacks suggesting TRY/CATCH or converting to a JSON
> and checking if the field exists, but I would think that there's a
> better way to check if the field is in the NEW record, no?


I assume using to_jsonb(new) and then check for the key in the json value 
will be faster than checking e.g. information_schema.column 
or pg_catalog.pg_attribute





view thread (10+ messages)  latest in thread

Message-ID: <37107788-df16-ca5f-3193-6176fe40f282@gmx.net>
Permalink:  ../37107788-df16-ca5f-3193-6176fe40f282@gmx.net/
Also on:    postgresql.org/message-id/37107788-df16-ca5f-3193-6176fe40f282@gmx.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: How to check if a field exists in NEW in trigger
  In-Reply-To: <37107788-df16-ca5f-3193-6176fe40f282@gmx.net>
  References: <0cc87310-590f-740e-977d-bcdde57eef1b@lucee.org> <37107788-df16-ca5f-3193-6176fe40f282@gmx.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox