How to temporarily violate a unique constraint? #10793
Unanswered
MarcelloTheArcane
asked this question in
Question
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following table:
I have a unique constraint
( title, chapter ), to make sure that you can't have duplicate chapters in a given book.How can I swap two chapters? If I try to do it with a mutation
I get a
Uniqueness violation. duplicate key value violates unique constraint books_title_chapter_indexerror.I have thought about updating in two stages, for example by updating one chapter to a known-impossible value (like -1) and then updating again to the swapped value, but this creates a race condition and leaves the database in an incorrect state if the second request fails for some reason.
Is it possible to have a post-update trigger that enforces the constraint? How would that react if someone tried an incorrect update?
Beta Was this translation helpful? Give feedback.
All reactions