prisma icon indicating copy to clipboard operation
prisma copied to clipboard

Prisma include Option Ignored When Using orderBy and distinct

Open Ali1Ammar opened this issue 4 months ago • 6 comments

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

  1. Create a Prisma query with include, orderBy, and distinct options:
const users = await prisma.user.findMany({
  include: {
    posts: true,
  },
  orderBy: [{ id: "asc" }, { name: "desc" }],
  distinct: ["id"],
});
  1. Try to access the included relation:
console.log(users[0].posts); // ❌ TypeScript Error
  1. Run the application:
npm run dev
Image

Reproduction

https://github.com/Ali1Ammar/prisma-bug

Expected vs. Actual Behavior

Expected Behavior

  • The include option should work correctly with orderBy and distinct
  • Users should have a posts property containing an array of Post objects
  • TypeScript should recognize the posts property on the User type

Actual Behavior

  • The include option 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

Ali1Ammar avatar Aug 11 '25 23:08 Ali1Ammar

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) { 
}

davecarlson avatar Aug 13 '25 07:08 davecarlson

no, its not

Image Image

Ali1Ammar avatar Aug 13 '25 07:08 Ali1Ammar

Hey @Ali1Ammar, please include the generator and datasource blocks to your schema.prisma snippet as well, thanks.

jkomyno avatar Aug 17 '25 12:08 jkomyno

@jkomyno edited ,Also you could access a complete minimal project here https://github.com/Ali1Ammar/prisma-bug

Ali1Ammar avatar Aug 17 '25 12:08 Ali1Ammar

+1, am running into this issue as well.

bencered avatar Oct 07 '25 21:10 bencered

prisma: 6.19.0

Having the same issue with arrays. Using it with objects works fine, though.

Lstmxx avatar Dec 04 '25 09:12 Lstmxx

same with the 7

sfroment avatar Dec 15 '25 13:12 sfroment