Skip to content

Include query repeats duplicate foreign keys in the IN list (7.x; 6.x deduplicates) #31122

Open

Description

@justforkixlabs

Package and version

prisma 7.10.1, @prisma/client 7.10.1, @prisma/adapter-pg 7.10.1

What happened?

When a findMany includes a to-one relation, the follow-up query for the related model puts every parent row's foreign key into the IN list, including duplicates. 50 posts by the same author produce IN ($1, …, $50) with the same value 50 times:

SELECT "User"."id", "User"."name" FROM "User"
WHERE "User"."id" IN ($1,$2,$3, … ,$50) OFFSET $51
-- params: [1,1,1, … ,1,"0"]

Prisma 6.19.2 (Rust engine) sends the same include with the values deduplicated: IN ($1) OFFSET $2.

What did you expect to happen?

The IN list contains each distinct key once, as in 6.x.

The cost is small per query (on PostgreSQL 17, planning + execution for the 50-duplicate list was ~0.18 ms vs ~0.12 ms for one value), but it grows with page size and with how many rows share a parent, it adds bind parameters toward the 32,767 limit, and deduplicating a key list client-side costs well under a microsecond.

Minimal reproduction

generator client {
  provider            = "prisma-client"
  output              = "./gen"
  moduleFormat        = "esm"
  importFileExtension = "ts"
}

datasource db {
  provider = "postgresql"
}

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

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  authorId Int
  author   User   @relation(fields: [authorId], references: [id])
}
import { PrismaClient } from "./gen/client.ts";
import { PrismaPg } from "@prisma/adapter-pg";

const prisma = new PrismaClient({
  adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL }),
  log: [{ emit: "event", level: "query" }],
});

const user = await prisma.user.create({ data: { name: "a" } });
await prisma.post.createMany({
  data: Array.from({ length: 50 }, (_, i) => ({ title: `p${i}`, authorId: user.id })),
});

prisma.$on("query", (e) => { if (e.query.includes('FROM "public"."User"')) console.log(e.query, e.params) });
await prisma.post.findMany({ include: { author: true } });
await prisma.$disconnect();

The logged User query has 50 placeholders and params holds the same id 50 times.

Environment

  • Node.js 24.21.0, macOS
  • PostgreSQL 17
  • Driver adapter: @prisma/adapter-pg 7.10.1
  • Default relationLoadStrategy (query)

Additional context

Possibly related: #22902 (findUnique batching repeating identical equality checks). Not checked on 8.0.0-rc.

Activity

  1. prisma-asks commented on Nov 19, 2026

    @prisma-asks

    Thanks for reporting this. We're looking into it now: whether we can reproduce it and what impact it has.

    You can follow where this stands here: https://asks.prisma.cool/public/asks/01a130e6-1c4d-734b-a6f2-5b271c73cce6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions