Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a remark about upsert’s race conditions #3910

Closed
wants to merge 5 commits into from

Conversation

dtinth
Copy link
Contributor

@dtinth dtinth commented Oct 18, 2022

Describe this PR

I have been bitten hard by this issue:

This is because I have assumed that Prisma’s upsert behavior would be equivalent to MongoDB’s behavior.

  • MongoDB’s upsert behavior is similar to using a ON DUPLICATE KEY or ON CONFLICT clause, so the database makes sure that 2 simulataneous upsert’s on the same document with same unique key are treated sequentially — one upsert results in an insert, and the other results in an update.
  • Prisma’s upsert behavior first does a SELECT followed by INSERT/UPDATE. If 2 simultaneous upserts are taking place, it could lead to a race condition where both upserts does not see an existing record and both attempts to insert, resulting in a P2002 error.

Changes

Document this behavior. This is the remark I wish I saw when I decided to use this method.

What issue does this fix?

prisma/prisma#3242

Any other relevant information

N/A

@vercel
Copy link

vercel bot commented Oct 18, 2022

@dtinth is attempting to deploy a commit to the Prisma Team on Vercel.

A member of the Team first needs to authorize it.

@garrensmith
Copy link

@dtinth thanks for this documentation. I've added more in-depth documentation here #3952

I'm going to close this issue since we going to use the other issue that will lead to more comprehensive documentation on this topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants