How to handle multiple unique constraints with onConflictDoUpdate?
My postgres table has 2 unique constraints:
During insert I am able to handle
unique_id
constraint, but how do I handle the other one?
4 Replies
I think target can take an array of values too
Something like this (not sure tho)
You are right, it can be an array. This may be fine to handle
unique_game
constraint. But again, now how to handle the unique_id
constraint. It is one or the other again.I am also stuck with this. I think (not sure) the array is for composite unique constraints, not multiple individual unique constraints where both have to be independently unique.
The only solution I know of at the moment is to basically do a select on the table first. If you get a result then you know its not unique and can do an update using the select results primary key.
This isn't ideal because of the extra database call. If you find a better way let me know.
Hey thanks for the help and example.
Yes you are right about the array. This did occur to me but it feels like an unnecessarily complex operation when upsert exists. On the other hand, haven't found theany other safe way to do this.
I will update here if I find something.