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