question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

upsert across HTTP requests has a race condition

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Reactions:28
  • Comments:37 (14 by maintainers)

github_iconTop GitHub Comments

24reactions
windowsdeveloperwannabecommented, Jul 8, 2021

Should Prisma’s upsert not being a real upsert be highlighted by the documentation? Or alternatively the method renamed to create_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.

12reactions
bartoszhernascommented, Apr 11, 2022

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?

Prisma’s Data Guide Learn how databases work, how to choose the right one, and how to use databases with your applications to their full potential.

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 😦

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found