I'm trying to handle an array of counters column in Postgres for example, let's say I have this table
name | counters |
---|---|
Joe | [1,3,1,0] |
and now I'm adding 2 values ("Ben", [1,3,1,0]) and ("Joe",[2,0,2,1])
I expect the query to sum between the 2 counters vectors on conflict ([1,3,1,0] + [2,0,2,1] = [3,3,3,1])
the expected result:
name | counters |
---|---|
Joe | [3,3,3,1] |
Ben | [1,3,1,0] |
I tried this query
insert into test (name, counters)
values ("Joe",[2,0,2,1])
on conflict (name)
do update set
counters = array_agg(unnest(test.counters) + unnest([2,0,2,1]))
but it didn't seem to work, what am I missing?