【问题标题】:postgresql trigger NEW row as a jsonpostgresql 将 NEW 行作为 json 触发
【发布时间】:2017-03-30 10:18:57
【问题描述】:

我正在编写一个触发器,我需要将 NEW 行转换为 json 并插入到另一个表中。我没说对。

     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;

    -----------------------------------------------------------

【问题讨论】:

  • new 已经是行了 - 你应该可以row_to_json(NEW.*) ?...
  • 什么问题,错误信息,问题?
  • row_to_json(NEW) 已经成功了,谢谢
  • 我真的希望这有一个更强大的后续 INSERT。我想几乎相同,但是然后从 newRecord 中选择属性,比如“first_name”和“age”之类的......但是,Postgres 不允许我的函数使用 json 运算符.喜欢:newRecord->>'first_name' 在尝试使用这些运算符时,我什至无法保存函数。

标签: postgresql stored-procedures triggers


【解决方案1】:

改变

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

newRecord :=  row_to_json(NEW.*);

应该有帮助

【讨论】:

  • @alowsarwar 我回答你的问题了吗?
  • 我看到这是一个较旧的答案,我怀疑它有效。您能否详细说明并说明您将如何从该变量newRecord 中选择单个属性?我无法在内部使用 JSON 运算符成功运行和存储任何函数。没有-> ->>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 2019-12-24
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2016-10-15
  • 1970-01-01
相关资源
最近更新 更多