upsert across HTTP requests has a race condition
See original GitHub issueBug description
Situation: I have a model with a unique string. At the beginning I have an empty database. I want to create or update an object of the model with upsert and check if the object exists by searching for the unique string.
If I call several upserts with the same unique string at the same time, I expect it to be created once and then updated if necessary. But it is created once and then the error occurs: Unique constraint failed on the fields.
Unique constraint failed on the fields: (`testId`).
How to reproduce
Expected behavior
I expect it to be created once and then updated the just created object, instead of crashing.
Prisma information
model Test {
id Int @default(autoincrement()) @id
testId String @unique
}
const prismaClient = new PrismaClient({
log: ["info", "warn", "error"],
});
const upsertPromises: Promise<Test>[] = [];
for (let i = 0; i < 2; i++) {
upsertPromises.push(
prismaClient.test.upsert({
where: { testId: "testId" },
create: {
testId: "testId",
},
update: {},
})
);
}
await Promise.all(upsertPromises);
Environment & setup
- OS: Mac OS,
- Database: PostgreSQL
- Node.js version: v13.3.0
- Prisma version: 2.3.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:28
- Comments:37 (14 by maintainers)
Top Results From Across the Web
Concurrent transactions result in race condition with unique ...
I found a bug today when two http requests had been made within the same second which caused this function to be called...
Read more >Race-condition while sending http request - Stack Overflow
I have a situation that I need to dispatch two post requests, synchronously, and the second depends on the response of the first,...
Read more >Handling API request race conditions in React
Sometimes, multiple requests are fired in parallel (competing to render the same view), and we just assume the last request will resolve last....
Read more >Race Condition in Web Applications - Wallarm
John still has 100 points on his balance, but somehow he managed to make three requests to the web application in three threads...
Read more >RESTful Web Services: Preventing Race Conditions - Medium
Conditional requests happen when the current ETag is supplied to a conditional request header, such as If-Match or If-None-Match , when the user ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Should Prisma’s
upsert
not being a real upsert be highlighted by the documentation? Or alternatively the method renamed tocreate_or_update
like in ActiveRecord. 🤔 It is quite the blocker when you start getting unique constraint errors from simple upserts.Prisma’s data guide pages even describe the real upsert so Google results can get quite confusing.
Who though that doing completely opposite what the DataGuide recommends is a good choice?
I understand rationale for supporting other mechanism that do not support native UPSERT, but then just make it clear in Docs, and underneath use whatever is best suited for each type of DB. You are using ForeignKeys in Postgres, even though MongoDB doesn’t have it right?
Ok, so let me learn, but then do NOT allow me to use the full potential of the DB, at the same time misleading me of what upsert does (dataguide says to use prisma’s upsert as example of how to use postgre’s UPSERT, which is a lie). What gives? You are teaching people to do X, and at the same time you do not do the X. You even make it hard/impossible to do the correct thing while using Prisma 😦