Working with Postgres 9.3, Python 2.7, psycopg2.
I have a table called SomeTable with a json field some_json_array and row_id key.
some_json_array looks something like this:
"[{'key': 'value_one'}, {'key': 'value_two'}, etc]"
I also have a function in which I'm trying to add some elements to the json array of SomeTable corresponding to the given row_id.
My code is as follows:
CREATE OR REPLACE FUNCTION add_elements (insertion_id smallint, new_elements_json json)
RETURNS void AS $$
BEGIN
UPDATE SomeTable
SET some_json_array = (SELECT array_to_json(array_agg(some_json_array) || array_agg(new_elements_json)))
WHERE row_id = insertion_id;
END;
$$ LANGUAGE plpgsql;
I get the following error:
Cannot use aggregate function in UPDATE
which I believe is complaining about array_agg().
How would I modify this function to make it work? I can stick the WHERE clause into the SELECT statement if necessary.