Let's say I have a table students with a column type jsonb where I store a list with students' additional emails. A student row looks like this
| student_id | name | emails |
|---|---|---|
| 1 | John Doe | [j.doe@email.com] |
I'm using the following query to update the emails column:
UPDATE students SET emails = emails || '["j.doe@email.com"]'::jsonb
WHERE student_id=1
AND NOT emails @> '["j.doe@email.com"]'::jsonb;
Once the column emails is filled, if I reuse query above with the parameter ["j.doe@email.com", "john@email.com"], the column emails would be update with repeated value:
| student_id | name | emails |
|---|---|---|
| 1 | Student 1 | [j.doe@email.com, j.doe@email.com, john@email.com] |
Is there a way to make sure that in the column emails I'll always have a jsonb list with only unique values ?