Re: Trigger function - Mailing list pgsql-novice

From hubert depesz lubaczewski
Subject Re: Trigger function
Date
Msg-id 20210727063805.GA28983@depesz.com
Whole thread Raw
In response to Trigger function  ("Nicolas Mitchell" <mitchelln@posteo.net>)
Responses Re: Trigger function  ("Nicolas Mitchell" <mitchelln@posteo.net>)
List 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



pgsql-novice by date:

Previous
From: "Nicolas Mitchell"
Date:
Subject: Trigger function
Next
From: "Nicolas Mitchell"
Date:
Subject: Re: Trigger function