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

fix: don't compact findUniqueOrThrow #3458

Merged
merged 3 commits into from Dec 1, 2022
Merged

Conversation

Weakky
Copy link
Member

@Weakky Weakky commented Nov 30, 2022

Overview

fixes prisma/prisma#16548

@miguelff
Copy link
Collaborator

miguelff commented Nov 30, 2022

Tried the following reproduction, from issue 16549, against this branch's binary engine:

Schema

generator client {
  provider        = "prisma-client-js"
  output          = "../node_modules/.prisma/client"
  engineType      = "binary"
  previewFeatures = []
}

datasource db {
  provider = "postgresql"
  url      = "postgresql://prisma:prisma@localhost:5432"
}

model Post {
  id        String   @id
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  title String

  @@map("posts")
}

index.ts

import { PrismaClient } from '.prisma/client';

const prisma = new PrismaClient({
  log: ["info", "error", "query", "warn"],
  __internal: {
    engine: {
      endpoint: "http://127.0.0.1:57581",
    },
  },
} as any);

const id1 = 'VALID_ID1';
const id2 = 'VALID_ID2';

async function setup(): Promise<void> {
  // 1. Clear DB
  console.log('DeleteMany:', await prisma.post.deleteMany());

  // 2. Add posts
  console.log('\nCreate posts:', await prisma.post.createMany({
    data: [
      {
        id: id1,
        title: 'New post',
      },
      {
        id: id2,
        title: 'New post 2!',
      }
    ],
  }));
}

async function find(id: string): Promise<void> {
  // 1. Query it with findUnique (no error)
  console.log('\nFindUnique:', await prisma.post.findUnique({
    where: { id },
  }));

  // 2. Query it with findUniqueOrThrow (crash??)
  console.log('\nFindUniqueOrThrow:', await prisma.post.findUniqueOrThrow({
    where: { id },
  }));
}

async function main(): Promise<void> {
  await setup();
  await Promise.all([id1, id2].map(find));
}

main().then(() => console.log('OK!')).catch((err) => console.error(err));

Output: OK!

However, if I issue another request to findUniqueOrThow with an invalid ID:

...
await Promise.all([id1, id2, "INVALID_ID3"].map(find));

I get:

  prisma:engine  {
  error: TypeError: Cannot read properties of undefined (reading '0')
      at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29021:53
      at Array.map (<anonymous>)
      at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29019:28
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
} +68ms
  prisma:client:request_handler  TypeError: Cannot read properties of undefined (reading '0')
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29021:53
    at Array.map (<anonymous>)
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29019:28
    at processTicksAndRejections (node:internal/process/task_queues:95:5) +0ms
prisma:error Cannot read properties of undefined (reading '0')
  prisma:client:request_handler  TypeError: Cannot read properties of undefined (reading '0')
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29021:53
    at Array.map (<anonymous>)
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29019:28
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  clientVersion: '0.0.0'
} +3ms
prisma:error Cannot read properties of undefined (reading '0')
  prisma:client:request_handler  TypeError: Cannot read properties of undefined (reading '0')
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29021:53
    at Array.map (<anonymous>)
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29019:28
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  clientVersion: '0.0.0'
} +0ms
prisma:error Cannot read properties of undefined (reading '0')
TypeError: Cannot read properties of undefined (reading '0')
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29021:53
    at Array.map (<anonymous>)
    at /Users/miguel/GitHub/prisma/prisma/reproductions/16549/node_modules/.prisma/client/runtime/index.js:29019:28
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  clientVersion: '0.0.0'
}

I think this might be a different problem. Will need to investigate tomorrow.

@Jolg42 Jolg42 added this to the 4.8.0 milestone Dec 1, 2022
@miguelff
Copy link
Collaborator

miguelff commented Dec 1, 2022

This can be merged as-is. The problem subject of my previous comment

...
await Promise.all([id1, id2, "INVALID_ID3"].map(find));

Is a different one and will be addressed by prisma/prisma#16574

@Weakky Weakky merged commit b2fb1b5 into main Dec 1, 2022
@Weakky Weakky deleted the fix/find-unique-throw-batching branch December 1, 2022 11:52
SevInf pushed a commit that referenced this pull request Dec 1, 2022
@nowlena
Copy link

nowlena commented Dec 2, 2022

I am seeing the following issue with batching:

  • prisma.user.findUnique({ where { id } }); -> batching occurs
  • prisma.user.findUnique({ where { id } }).posts(); -> batching occurs
  • prisma.user.findUniqueOrThrow({ where { id } }); -> batching does NOT occur
  • prisma.user.findUniqueOrThrow({ where { id } }).posts(); -> batching does NOT occur

example based on Query optimization page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression bug: batched findUniqueOrThrow queries throw 'Field does not exist on enclosing type.'
4 participants