Is it possible to do upsert
in Postgres 9.5 when conflict happens on one of 2 columns in a table.? Basically I have 2 columns and if either column throws unique constraint violation, then I would like to perform update operation.
Yes, and this behaviour is default. Any unique constraint violation constitutes a conflict and then the UPDATE
is performed if ON CONFLICT DO UPDATE
is specified. The INSERT
statement can have only a single ON CONFLICT
clause, but the conflict_target
of that clause can specify multiple column names each of which must have an index, such as a UNIQUE
constraint. You are, however, limited to a single conflict_action
and you will not have information on which constraint caused the conflict when processing that action. If you need that kind of information, or specific action depending on the constraint violation, you should write a trigger function but then you lose the all-important atomicity of the INSERT ... ON CONFLICT DO ...
statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With