Can someone help me with the syntax of the following function?
In short, I want to update an existing JSON array with a new JSON entry in a specified column, with a matching ID ('projectid'). This works when the column name (e.g. my_column) is specified in SET, like this:
create function append_entry_to_dev (projectid int, entry jsonb)
returns void as
$$
update projects_develop
set my_column = coalesce(my_column, '[]'::jsonb) || to_jsonb(entry)
where id = projectid
returning *;
$$
language sql;
However, I'd like it if the column name in SET can be specified programmatically when the data is added, something like this — note the columnname is part of the call:
create function append_entry_to_dev (projectid int, entry jsonb, columnname text)
returns void as
$$
update projects_develop
set columnname = coalesce(columnname, '[]'::jsonb) || to_jsonb(entry)
where id = projectid
returning *;
$$
language sql;
When I try doing this, I get the error column "columnname" does not exist
If what I'm thinking is possible, can someone help me with the syntax to make it work as expected?