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.

The findMany query fails silently when the array length passed through IN parameter exceeds 999

See original GitHub issue

Bug description

The findMany query fails silently when the array length passed through IN parameter exceeds 999.

How to reproduce

const tags999 = await prisma.tag.findMany({
  where: {
    id: {
      in: Array.from({length: 999}, () => Math.floor(Math.random() * 1000))
    }
  },
}) 
console.log(tags999)
// Returns [{obj1}, {obj2}, ....] which is correct.
const tags1000 = await prisma.tag.findMany({
  where: {
    id: {
      in: Array.from({length: 1000}, () => Math.floor(Math.random() * 1000))
    }
  },
}) 
console.log(tags1000)
// Returns [] which is wrong.
// We need a proper error message here, something like "You have reached the max length of array passed to IN parameter."

A related bug occurs when we use relation queries with more than 1000 tags added to all posts in total:

const posts = await prisma.post.findMany({
  include: {
    postTags: {
      include: {
        tag: true
      }
    }
  }
})

Which causes the following error:

Invalid prisma.post.findMany() invocation: Inconsistent query result: Field tag is required to return data, got null instead.

It probably happens because the relation query somehow uses IN subquery. It’s my guess based on how this kind of relation query is implemented in another framework.

Expected behavior

Remove this hardcoded limit for < 1000 items in the IN array. Let user decide what’s safe for them!

Alternatively:

  1. The shown IN query with more than 1000 items passed should fail with a clear error message.
  2. The described relation query should produce a better error message.

Prisma information

model Post {
  id                      Int           @id @default(autoincrement()) @db.UnsignedInt
 
  postTags          PostTag[]

  @@map("posts")
}
model Tag {
  id                      Int             @id @default(autoincrement()) @db.UnsignedInt
  
  postTags           PostTag[]

  @@map("tags")
}

model PostTag {
  postId                 Int             @map("post_id") @db.UnsignedInt
  tagId                   Int             @map("tag_id") @db.UnsignedInt

  post                   Post           @relation(fields: [postId], references: [id], onDelete: Cascade)
  tag                     Tag             @relation(fields: [tagId], references: [id], onDelete: Cascade)

  @@id([postId, tagId])

  @@map("posts_tags")
}

Environment & setup

  • OS: Docker: node:16-alpine (Linux)
  • Database: Docker: mariadb:10.7
  • Node.js version: v16.14.2

Prisma Version

    "@prisma/client": "^4.1.1"
    "prisma": "^4.1.1"

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
kkomelincommented, Jul 30, 2022

The worst things is you can’t expect that. You start working on your app and everything works perfectly. Data is displayed. Search filters are working. You happily deploy the app to production and sleep well… until one day everything breaks suddenly. The data is not on the screen anymore. And the logs are clean. The reason is trivial - when your database grows, IN select queries stop working… not all at once, just some of them in which IN array length exceeds 999. And tomorrow any other IN queries can gracefully return empty result.

No app should behave that unpredictably, so for me it’s a high priority.

2reactions
kkomelincommented, Sep 1, 2022

Already. Thanks to your tests @jkomyno I have found the reason of the problem: https://github.com/prisma/prisma/pull/15124#issuecomment-1233913074

Read more comments on GitHub >

github_iconTop Results From Across the Web

prisma findmany where not null - You.com | The Search Engine You ...
Prisma findMany function is not returning relational data ... The findMany query fails silently when the array length passed through IN parameter exceeds...
Read more >
Prisma 4.4.0 Release - GitClear
Ordered by the degree to which they evolved the repo in this version. ... The findMany query fails silently when the array length...
Read more >
Cannot get the Length of an Array in an extra Function in C++
You cannot determine the length of an array that is passed to you like that. You need to also pass the length as...
Read more >
Konstantin Komelin в LinkedIn: How to Protect Your CMS Site from ...
The findMany query fails silently when the array length passed through IN parameter exceeds 999 · Issue #14539 · prisma/prisma. github.com.
Read more >
1177 Error: Operation name <name> exceeds maximum length.
Powered by Zoomin Software. For more details please contactZoomin · Rocket Software Documentation · Home · Library · Register · Login ...
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