Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql trigger NEW row as a json

I am writing a trigger where I need to convert NEW row into a json and insert into an other table. I am not getting it right.

     CREATE OR REPLACE FUNCTION public.eventlogs_add_user()
      RETURNS trigger AS

    $BODY$
    DECLARE newRecord JSON;
    BEGIN

    newRecord :=  row_to_json(row)
        from (NEW) row;


// Insert newRecord into another table


    RETURN NEW;
    END$BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    ALTER FUNCTION public.eventlogs_add_user()
      OWNER TO postgres;

    -----------------------------------------------------------
like image 481
mbarish-me Avatar asked Mar 30 '17 10:03

mbarish-me


1 Answers

changing

newRecord :=  row_to_json(row)
        from (NEW) row;

to

newRecord :=  row_to_json(NEW.*);

should help

like image 58
Vao Tsun Avatar answered Oct 26 '22 05:10

Vao Tsun