触发函数执行但不插入数据

时间:2019-09-09 21:20:38

标签: sql json postgresql

我有一个在插入时为具有JsonB类型的单列的表触发的触发函数。

我尝试了new。*功能,并尝试运行失败

触发功能:

CREATE OR REPLACE FUNCTION public.extractjsondata()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF 
AS $BODY$
DECLARE
   message_id text;
   event_name text;
   docebo_user_id int;
   webhook_fired_at text;
   docebo_course_id int;
   docebo_course_completion_date text;
BEGIN
   message_id = (select new.jsonpayload -> 'message_id'
                 FROM public."LMSWebHookData");
   event_name = (select new.jsonpayload ->  'event'
                 FROM public."LMSWebHookData");
   docebo_user_id = (select new.jsonpayload -> 'payload' ->> 'user_id'
                     FROM public."LMSWebHookData");
   webhook_fired_at = (select new.jsonpayload -> 'payload' ->> 'fired_at'
                       FROM public."LMSWebHookData");
   docebo_course_id = (select new.jsonpayload -> 'payload' ->> 'course_id'
                       FROM public."LMSWebHookData");
   docebo_course_completion_date = (select new.jsonpayload -> 'payload' ->> 'completion_date'
                                    FROM public."LMSWebHookData");
   INSERT INTO EducationHistory
      (message_id, event_name, docebo_user_id, webhook_fired_at, docebo_course_id, docebo_course_completion_date)
   VALUES
      (message_id, event_name, docebo_user_id, webhook_fired_at, docebo_course_id, docebo_course_completion_date);
   Return New; 
END;
$BODY$;

触发:

CREATE TRIGGER "extract_jsondata_into_EducationHistory"
    AFTER INSERT OR UPDATE 
    ON public."LMSWebHookData"
    FOR EACH ROW
    EXECUTE PROCEDURE public.extractjsondata();

0 个答案:

没有答案