Skip to content

[BUG] Polymorphic relationships with @@delegate fail when using where clauses in nested queries #2246

@jagodin

Description

@jagodin

Description and expected behavior

When using polymorphic relationships with @@delegate, queries fail with PrismaClientValidationError when trying to use where clauses on delegated fields in nested queries.

Queries with where clauses on delegated fields should and not throw any Prisma errors.

Reproduction

I've created a minimal reproduction: https://codesandbox.io/p/github/jagodin/zenstack-polymorphic-delegate-bug/main

Schema:

model Media {
  id Int @id @default(autoincrement())
  title String
  mediaType String
  
  @@delegate(mediaType)
  @@allow('all', true)
}

model Movie extends Media {
  director Director @relation(fields: [directorId], references: [id])
  directorId Int
  duration Int
  rating String
}

model Director {
  id Int @id @default(autoincrement())
  name String
  email String
  movies Movie[]
  
  @@allow('all', true)
}

Working query:

await prisma.director.findMany({
  include: {
    movies: {
      orderBy: { title: 'asc' } // ✅ Works
    }
  }
})

Failing query:

await prisma.director.findMany({
  include: {
    movies: {
      where: { title: 'Inception' } // ❌ Fails
    }
  }
})

Environment (please complete the following information):

package.json:

{
    "name": "prisma-zenstack-polymorphic-relation-type-error",
    "version": "1.0.0",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "reproduce": "npx tsx src/index.ts",
        "setup": "npx zenstack generate && npx prisma migrate dev --name init"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "description": "",
    "devDependencies": {
        "@types/node": "^24.3.1",
        "prisma": "6.14.x",
        "tsx": "^4.20.5",
        "typescript": "^5.9.2",
        "zenstack": "2.18.1"
    },
    "dependencies": {
        "@prisma/client": "6.14.x",
        "@zenstackhq/runtime": "2.18.1"
    }
}

Additional context
Add any other context about the problem here.

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