Re: Trigger function

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Nicolas Mitchell <mitchelln(at)posteo(dot)net>
Cc: pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Re: Trigger function
Date: 2021-07-27 06:38:05
Message-ID: 20210727063805.GA28983@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Mon, Jul 26, 2021 at 03:17:12PM +0000, Nicolas Mitchell wrote:
> I am looking at whether functions can help me automate creating a new object
> when a new host is added.
>
> I can do this manually with the following code:
>
> WITH object_id AS
> (INSERT INTO public.object (type)
> VALUES (
> ( SELECT obtype.id
> FROM public.obtype
> WHERE obtype.name LIKE 'host'
> )
> )
> RETURNING id)
> INSERT INTO host (name, domain, object)
> VALUES ('gary', 1000001, (SELECT * FROM object_id));
>
> I have a number of questions but I would like to begin by asking whether
> this a candidate for a trigger function on table ‘host’, triggered before an
> insert?

*after* insert, not *before*.
But other than that yes.

If I might suggest:
https://www.depesz.com/2012/11/14/how-i-learned-to-stop-worrying-and-love-the-triggers/

depesz

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Nicolas Mitchell 2021-07-27 21:05:26 Re: Trigger function
Previous Message Nicolas Mitchell 2021-07-26 15:17:12 Trigger function