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

calling findUnique concurrently on a model with a compound unique constraint causes it to return null #15934

Closed
MichaelB-99 opened this issue Oct 21, 2022 · 0 comments · Fixed by prisma/prisma-engines#3328
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: extendedWhereUnique Preview Feature topic: findUnique() topic: previewFeatures Issue touches on an preview feature flag
Milestone

Comments

@MichaelB-99
Copy link

Bug description

This bug occurs when running multiple findUnique queries with another non unique value (extendedWhereUnique) concurrently on a model with a compound unique constraint. It causes prisma to return null instead of the record.

  • Only occurs when calling findUnique with the unique field and another non unique field
  • Only occurs when the model has a @@unique() constraint
  • Only occurs when findUnique queries are run concurrently
  • Note that if filtering on the non unique value e.g deletedAt: { not:null }, we get the following rust error : tokio-runtime-worker' panicked at 'called Option::unwrap() on a None value', query-engine\core\src\response_ir\mod.rs:55:43

How to reproduce

I have made a repo with a reproduction here: https://github.com/MichaelB-99/prisma-bug-reproduction.

Expected behavior

That findUnique returns the record regardless of if it is run concurrently or not

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["extendedWhereUnique"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Post {
  id        Int
  userId    Int
  text      String
  deletedAt DateTime?

  @@unique([id, userId])
}
import { PrismaClient } from "@prisma/client";

async function main() {
  const prisma = new PrismaClient();
  await prisma.post.deleteMany({});
  await prisma.post.create({
    data: {
      id: 1,
      userId: 1,
      text: "Hello, world!",
    },
  });

  const queryPost = () =>
    prisma.post.findUnique({
      where: {
        id_userId: {
          id: 1,
          userId: 1,
        },
        // without non unique values the error doesn't occur
        deletedAt: null,
      },
    });

    
    // if we change the length to one to run this once then we get the expected result
  // {concurrent: [ { id: 1, userId: 1, text: 'Hello, world!', deletedAt: null } ]}

  const concurrent = await Promise.all(Array.from({ length: 2 }, queryPost));
  console.log({ concurrent });

  // { concurrent: [ null, null ] }
}

main().catch((e) => console.error(e));

Environment & setup

  • OS: Windows 10
  • Database: PostgreSQL,
  • Node.js version: v17.9.0

Prisma Version

prisma                  : 4.5.0
@prisma/client          : 4.5.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\query_engine-windows.dll.node)      
Migration Engine        : migration-engine-cli 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\migration-engine-windows.exe)  
Introspection Engine    : introspection-core 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Format Wasm             : @prisma/prisma-fmt-wasm 4.5.0-43.0362da9eebca54d94c8ef5edd3b2e90af99ba452
Default Engines Hash    : 0362da9eebca54d94c8ef5edd3b2e90af99ba452
Studio                  : 0.476.0
Preview Features        : extendedWhereUnique
@MichaelB-99 MichaelB-99 added the kind/bug A reported bug. label Oct 21, 2022
@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. topic: findUnique() topic: previewFeatures Issue touches on an preview feature flag topic: extendedWhereUnique Preview Feature labels Oct 24, 2022
@Weakky Weakky self-assigned this Oct 28, 2022
@janpio janpio added this to the 4.6.0 milestone Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: extendedWhereUnique Preview Feature topic: findUnique() topic: previewFeatures Issue touches on an preview feature flag
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants