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.

Running multiple findUnique's in parallel causes both to return null

See original GitHub issue

Bug description

When running two of the same findUnique queries in parallel, both return null where they should return the same document.

How to reproduce

  1. Create a new Prisma project with a PostgreSQL database.
  2. Add a table with a @@unique attribute
datasource db {
  provider = "postgresql"
  url      = "URL"
}

generator client {
  provider = "prisma-client-js"
}

model Property {
  id           String         @id @default(cuid())
  Availability Availability[]
}

model Availability {
  date          DateTime
  property      Property              @relation(fields: [propertyId], references: [id])
  propertyId    String
  status        AvailabilityStatus    @default(AVAILABLE)

  @@unique([propertyId, date, status])
}

enum AvailabilityStatus {
  AVAILABLE
}
  1. Create a new set of documents in the table
const property = await prisma.property.create({});

const targetDate = new Date('2020-12-03T00:00:00.000Z');

await prisma.availability.create({
    data: {
      property: { connect: {id: property.id }},
      status: AvailabilityStatus.AVAILABLE,
      date: targetDate,
    },
  });
  1. Query the document twice in parallel
const firstUnique = prisma.availability.findUnique({
    where: {
      propertyId_date_status: {
        propertyId: property.id,
        status: AvailabilityStatus.AVAILABLE,
        date: targetDate,
      }
    },
  });

  const secondUnique = prisma.availability.findUnique({
    where: {
      propertyId_date_status: {
        propertyId: property.id,
        status: AvailabilityStatus.AVAILABLE,
        date: targetDate,
      }
    },
  });

  const res = await Promise.all([firstUnique, secondUnique])

  console.log(res); <-- [null, null]

Expected behavior

Expected to return the same document twice - a warning would also be super helpful if this happens.

Prisma information

Environment & setup

  • OS: Mac
  • Database: PostgreSQL
  • Node.js version: 12.18.0
  • Prisma version: 2.12.1
@prisma/cli          : 2.12.1
@prisma/client       : 2.12.1
Current platform     : darwin
Query Engine         : query-engine cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt cf0680a1bfe8d5e743dc659cc7f08009f9587d58 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.322.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
nrxuscommented, Jan 26, 2021

We just run into this behavior and it was incredibly surprising. The most disappointing part is how how silent this error is. There is no warning in the prisma logs, nothing documented about using composite keys with findUnique not working when doing things in parallel. While I understand that this bug may be hard to resolve, at the very least some loud logging or just a panic so it can be caught quickly would be the least I would expect from a tool that should be prod-ready.

EDIT: In retrospect my comment comes accross a little too aggressive/accusatory, I apologize for my tone. Commenting after spending a long time debugging got the better of me.

1reaction
nrxuscommented, Jan 29, 2021

I do see it fixed in 2.15! Thank you so much!

I should have tried updating my prisma before commenting, I was on 2.14 and since I didn’t see anything in the changelog for 2.15 about this I assumed it was still an issue 🤦🏽

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# List.Find() inside Parallel.ForEach Sometimes Returns NULL
I know writing to a common variable from multiple threads is problematic (race conditions) but I can't see why reading from a common...
Read more >
Data Loading - Remix
This can get a little tricky with React's controlled component concept. This is only needed if the search params can be set in...
Read more >
Issues with NULL values in input columns in Parallel ... - IBM
To test if a value is NULL in a logical expression, use one of these two functions. IsNotNull(); IsNull(). For example: DSLink3.OrderCount +...
Read more >
Null and undefined (Reference) - Prisma
When this query is run no data is returned. This is in contrast to the AND and NOT operators, which will both return...
Read more >
Check if a Value Is Null or Undefined in JavaScript or Node.js
When „empty“ we mean that they don't have an exact value assigned. These two values are null and undefined . Both values, null...
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