Skip to content

BlooJeans/prisma-issue-concurrent-findUnique-keyorder-matters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

There is an issue with Prisma which causes concurrent usages of findUnique() to return null values if the calls specify the uniqueWhereInput keys in different orders

Tracked Issue: Prisma Issue #16267

  • Only happens with findUnique or findUniqueOrThrow
  • Only when multiple findUnique calls are made concurrently and Prisma runs them in a batched query.
  • The uniqueWhereInput needs to be different for the problem to occur

Example:

  const funcA = () =>
    prisma.relationship.findUnique({
      where: {
        fooId_barId: {
          // note the order of these two keys
          fooId,
          barId,
        },
      },
    });

  const funcB = () =>
    prisma.relationship.findUnique({
      where: {
        fooId_barId: {
          // note the order of these two keys
          barId,
          fooId,
        },
      },
    });

  const test1 = await Promise.all([funcA(), funcA()]);
  // test1[0]: found
  // test1[1]: found

  const test2 = await Promise.all([funcB(), funcB()]);
  // test2[0]: found
  // test2[1]: found

  const test3 = await Promise.all([funcA(), funcB()]);
  // test3[0]: found
  // test3[1]: null (!!)

  const test4 = await Promise.all([funcB(), funcA()]);
  // test4[0]: found
  // test4[1]: null (!!)

npm run test output

runTest 1: consistent order
{
  seqRow1: true,
  seqRow2: true,
  concurrentRow1: true,
  concurrentRow2: true
}

runTest 2: different, but still consistent order
{
  seqRow1: true,
  seqRow2: true,
  concurrentRow1: true,
  concurrentRow2: true
}

runTest 3: differing orders
{
  seqRow1: true,
  seqRow2: true,
  concurrentRow1: true,
  concurrentRow2: false
}
!!!!!!!! Error: concurrent usage did not match

runTest 4: reverse but still different orders
{
  seqRow1: true,
  seqRow2: true,
  concurrentRow1: true,
  concurrentRow2: false
}
!!!!!!!! Error: concurrent usage did not match

Workaround

  • Be consistent with all usages of findUnique
  • Switch one of the concurrent calls to use findFirst instead

Versions Tested

  • 4.0.0
  • 4.5.0
  • 4.6.1

Steps for reproduction

  1. Clone this repository
  2. Change the credentials in the .env file
  3. Run npm install to install dependencies
  4. Run npm run prisma migrate dev to bootstrap database
  5. Run npm run start to reproduce

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published