It would be nice if PostgreSQL has *_extend (similar to $.extend from
jQuery or _.extend from UnderscoreJs) for JSON/JSONB columns, so we could
write it like this:

UPDATE tablename
  SET jsoncolumn = json_extend(jsoncolumn, $2)
  WHERE id = $1;

instead of this:

UPDATE tablename
  SET data = (
    SELECT json_object_agg(key, value)::jsonb
      FROM
        ( SELECT * FROM jsonb_each( SELECT data FROM tablename WHERE id=$1 )
          UNION ALL
          SELECT * FROM jsonb_each( $2 )
        ) x1
  ) WHERE id=$1;

Reply via email to