Suppose I have an update query for a table with constraints (Postgres 11).
I do not know the constraints so I won’t be able to determine whether an update query will fail or not.
If an update query failes due to a conflict I want to do nothing and return 0 (no rows were updated).
With an insert you can have ‘INSERT INTO … ON CONFLICT DO NOTHING’ but this option does not exist in an update query.
Is there a solution?
2
Answers
You could try using a CTE to get the result of the the
UPDATE
statement, and then return the updated rows usingRETURNING *
, but if it fails due to a conflict, it returns nothing.Not a direct answer but there is no reason to make a statement like: I do not know … so I won’t be able to …. Instead that should prompt the question: How do I …. In this case that would be How do I determine the constraints on a table? The the following is one such way:
Then with that information you can pose a better question to your issue, or make the question mute.