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

Error Thrown When Sorting Field Is Not Selected #24152

Open
Hellio404 opened this issue May 10, 2024 · 3 comments
Open

Error Thrown When Sorting Field Is Not Selected #24152

Hellio404 opened this issue May 10, 2024 · 3 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: chunking topic: orderBy / sorting topic: rust panic topic: sql server Microsoft SQL Server

Comments

@Hellio404
Copy link

Hellio404 commented May 10, 2024

Hi Prisma Team! My Prisma Client just crashed. This is the report:

Versions

Name Version
Node v20.10.0
OS darwin-arm64
Prisma Client 5.3.1
Query Engine 61e140623197a131c2a6189271ffee05a7aa9a59
Database sqlserver

Logs

PrismaClientRustPanicError:
Invalid `client.post.findMany()` invocation in
/build/index.js

  61         })];
  62 case 1:
  63     id = (_a.sent()).id;
→ 64     return [4 /*yield*/, client.post.findMany(
no entry found for key

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

Client Snippet

  await client.user.create({
    data: {
      posts: {
        createMany: {
          data: Array(4000).fill({title: 'post'}),
        }
      }
    }
  });

  await client.post.findMany({
    where: { authorId: { in: Array.from(Array(4000).keys()) }},
    select: {id: true, title: true},
    orderBy: {createdAt: 'asc'},
  });

Schema

// PLEASE ADD YOUR SCHEMA HERE IF POSSIBLE
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider          = "sqlserver"
  url               = env("DATABASE_URL")
  shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}

model User {
  id       Int      @id @default(autoincrement())
  posts    Post[]
}

model Post {
  id        Int    @id @default(autoincrement())
  title     String
  author    User   @relation(fields: [authorId], references: [id])
  authorId  Int
  createdAt DateTime @default(now())
}

Description

I've noticed that whenever a query is split (due to the maximum parameter constraint of 2100 in SQL Server), Prisma attempts to perform an in-memory sort.
However, if the field being sorted is not selected, an error is thrown.

To fix this issue, I had to always include the field used for sorting in the selected fields.

@Druue Druue added team/client Issue for team Client. topic: rust panic bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. labels May 13, 2024
@Druue
Copy link
Contributor

Druue commented May 13, 2024

Hey @Hellio404, I can confirm the ""2100 parameters"" error message but I can't confirm the resolution by including the createdAt: true in the select, neither on dev nor on 5.3.1.

await prisma.post.findMany({
  where: { authorId: { in: Array.from(Array(4000).keys()) } },
-  select: { id: true, title: true },
+  select: { id: true, title: true, createdAt: true },
  orderBy: { createdAt: 'asc' },
});

Could you please provide a reproduction that shows this succeeding with the inclusion of createdAt in select

@Hellio404
Copy link
Author

@Druue

I double checked and I confirm that adding the createdAt: true fixes the issue for me as well as removing select all together

  const { id } = await client.user.create({
    data: {
      posts: {
        createMany: {
          data: Array(4000).fill({title: 'post'}),
        }
      }
    }
  });
  // this works
  const sortedPosts = await client.post.findMany({
    where: { authorId: { in: Array.from(Array(4000).keys()) }},
    select: {id: true, title: true, createdAt: true},
    orderBy: {createdAt: 'asc'},
  });
  // this works too
  await client.post.findMany({
    where: { authorId: { in: Array.from(Array(4000).keys()) }},
    orderBy: {createdAt: 'asc'},
  });

Druue added a commit to Druue/prisma-debug that referenced this issue May 14, 2024
@Druue
Copy link
Contributor

Druue commented May 14, 2024

Huh, I double checked in another repro repo and I can reproduce it there. You can see a link above. For some reason, however, I still cannot reproduce this in our internal reproductions dir that directly hooks into a locally-built version of the client. I have no idea what's going on there :/

Looking at the succeeding findMany query that includes createdAt: true, I see following two (minimised) SQL queries logged.

SELECT [dbo].[Post].[id], [dbo].[Post].[title], [dbo].[Post].[createdAt]
FROM [dbo].[Post]
WHERE [dbo].[Post].[authorId] IN (@P1 ... @P2098)

SELECT [dbo].[Post].[id], [dbo].[Post].[title], [dbo].[Post].[createdAt]
FROM [dbo].[Post]
WHERE [dbo].[Post].[authorId] IN (@P1 ... @P1902)

If I swap to the failing query that doesn't include createdAt: true, I see the following logged

SELECT [dbo].[Post].[id], [dbo].[Post].[title]
FROM [dbo].[Post]
WHERE [dbo].[Post].[authorId] IN (@P1 ... @P2098)

with the following error:

thread 'tokio-runtime-worker' panicked at query-engine/query-structure/src/record.rs:69:46:
no entry found for key
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This is the relevant code that it references

and then the following SQL query after:

SELECT [dbo].[Post].[id], [dbo].[Post].[title]
FROM [dbo].[Post]
WHERE [dbo].[Post].[authorId] IN (@P1 ... @P1902)

@Druue Druue added bug/2-confirmed Bug has been reproduced and confirmed. topic: orderBy / sorting and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: chunking topic: orderBy / sorting topic: rust panic topic: sql server Microsoft SQL Server
Projects
None yet
Development

No branches or pull requests

3 participants