Re: How to check if a field exists in NEW in trigger

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: "Igal (at) Lucee(dot)org" <igal(at)lucee(dot)org>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to check if a field exists in NEW in trigger
Date: 2019-08-05 00:29:40
Message-ID: 9bcf31ea-73f2-2c88-19dd-f5c4140aacc1@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 8/4/19 3:52 PM, Igal @ Lucee.org wrote:
> 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,

Do you mean:

1) There is no actual email column?

2) There is an email column but no value for it?

> 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

Is:

on_record_modified()

a generic function that will be applied to many tables with differing
schema?

>
> 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?

Use

TG_RELID

and look up the columns in:

https://www.postgresql.org/docs/11/catalog-pg-attribute.html

Or use the columns information_schema:

https://www.postgresql.org/docs/11/infoschema-columns.html

>
> Any ideas?  Thanks!
>
> Igal Sapir
> Lucee Core Developer
> Lucee.org <http://lucee.org/>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2019-08-05 05:55:29 Re: How to check if a field exists in NEW in trigger
Previous Message Igal @ Lucee.org 2019-08-04 22:52:22 How to check if a field exists in NEW in trigger