Prisma include Option Ignored When Using orderBy and distinct
Bug description
Prisma include Option Ignored When Using orderBy and distinct
Summary
When using Prisma's findMany query with both orderBy and distinct options, the include option is completely ignored, causing TypeScript compilation errors.
Steps to Reproduce
- Create a Prisma query with
include,orderBy, anddistinctoptions:
const users = await prisma.user.findMany({
include: {
posts: true,
},
orderBy: [{ id: "asc" }, { name: "desc" }],
distinct: ["id"],
});
- Try to access the included relation:
console.log(users[0].posts); // ❌ TypeScript Error
- Run the application:
npm run dev
Reproduction
https://github.com/Ali1Ammar/prisma-bug
Expected vs. Actual Behavior
Expected Behavior
- The
includeoption should work correctly withorderByanddistinct - Users should have a
postsproperty containing an array of Post objects - TypeScript should recognize the
postsproperty on the User type
Actual Behavior
- The
includeoption is completely ignored - Users only have the base properties (
id,email,name) - TypeScript compilation fails with error:
Property 'posts' does not exist on type '{ id: number; name: string | null; email: string; }'
Frequency
Consistently reproducible
Does this occur in development or production?
Only in development (e.g., CLI tools, migrations, Prisma Studio)
Is this a regression?
Workaround
Prisma Schema & Queries
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
const users = await prisma.user.findMany({
include: {
posts: true,
},
orderBy: [{ id: "asc" }, { name: "desc" }],
distinct: ["id"],
});
Prisma Config
// Add your `prisma.config.ts`
Logs & Debug Info
// Debug logs here
Environment & Setup
- Prisma Version: 6.9.0
- Prisma Extension: @prisma/extension-accelerate 2.0.2
- Node.js: v24.3.0
- TypeScript: 5.8.2
- ts-node: v10.9.2
- Database: PostgreSQL
Prisma Version
aliammar@ALiAmmarMac express % npx prisma -v Prisma schema loaded from prisma/schema.prisma prisma : 6.9.0 @prisma/client : 6.9.0 Computed binaryTarget : darwin-arm64 Operating System : darwin Architecture : arm64 Node.js : v24.3.0 TypeScript : 5.8.2 Query Engine (Node-API) : libquery-engine 81e4af48011447c3cc503a190e86995b66d2a28e (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node) Schema Engine : schema-engine-cli 81e4af48011447c3cc503a190e86995b66d2a28e (at node_modules/@prisma/engines/schema-engine-darwin-arm64) Schema Wasm : @prisma/prisma-schema-wasm 6.9.0-10.81e4af48011447c3cc503a190e86995b66d2a28e Default Engines Hash : 81e4af48011447c3cc503a190e86995b66d2a28e Studio : 0.511.0
I believe this is because you aren't checking that your array of users has anything in it. You've assumed this will always return at least 1 result. What about if there are none ? then users[0] would be invalid.
wrap it in a
if (users.length > 0) {
}
no, its not
Hey @Ali1Ammar, please include the generator and datasource blocks to your schema.prisma snippet as well, thanks.
@jkomyno edited ,Also you could access a complete minimal project here https://github.com/Ali1Ammar/prisma-bug
+1, am running into this issue as well.
prisma: 6.19.0
Having the same issue with arrays. Using it with objects works fine, though.
same with the 7