Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong nullable types with fluent API in Prisma 5.0 #20183

Closed
p-kuen opened this issue Jul 12, 2023 · 4 comments · Fixed by #20197
Closed

Wrong nullable types with fluent API in Prisma 5.0 #20183

p-kuen opened this issue Jul 12, 2023 · 4 comments · Fixed by #20197
Assignees
Labels
5.0.0 bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: fluent api topic: prisma-client
Milestone

Comments

@p-kuen
Copy link

p-kuen commented Jul 12, 2023

Bug description

We use Fluent API very often and it works very well.
Since Prisma version 5.0, results for related models queried with fluent API are nullable, although findUniqueOrThrow was used on the base model and the relation field is not optional.

How to reproduce

  1. Create two models with a 1:n relation:
model PlanGroup {
  id             Int                     @id @default(autoincrement()) @db.UnsignedInt
  plans          BuildingSitePlan[]
}

model Plan {
  id              Int         @id @default(autoincrement()) @db.UnsignedInt
  groupId         Int         @map("group_id") @db.UnsignedInt
  group           PlanGroup   @relation(fields: [groupId], references: [id], onDelete: Cascade)
}
  1. Use Fluent API to query them:
(
    await prisma.plan
      .findUniqueOrThrow({
        where: {
          id,
        },
      })
      .group({
        select: {
          id: true,
        },
        
      })
  ).id
  1. See typescript error ts(2531) "Object is possibly null"

Expected behavior

In versions <5.0.0, queries for non-nullable related models with fluent api were non-nullable.

Prisma information

model PlanGroup {
  id             Int                     @id @default(autoincrement()) @db.UnsignedInt
  plans          BuildingSitePlan[]
}

model Plan {
  id              Int         @id @default(autoincrement()) @db.UnsignedInt
  groupId         Int         @map("group_id") @db.UnsignedInt
  group           PlanGroup   @relation(fields: [groupId], references: [id], onDelete: Cascade)
}
(
    await prisma.plan
      .findUniqueOrThrow({
        where: {
          id,
        },
      })
      .group({
        select: {
          id: true,
        },
        
      })
  ).id

Environment & setup

  • OS: macOS Ventura 13.2
  • Database: MariaDB 10.11.3
  • Node.js version: v20.2.0

Prisma Version

prisma                  : 5.0.0
@prisma/client          : 5.0.0
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 6b0aef69b7cdfc787f822ecd7cdc76d5f1991584 (at node_modules/.pnpm/@prisma+engines@5.0.0/node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 6b0aef69b7cdfc787f822ecd7cdc76d5f1991584 (at node_modules/.pnpm/@prisma+engines@5.0.0/node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 4.17.0-26.6b0aef69b7cdfc787f822ecd7cdc76d5f1991584
Default Engines Hash    : 6b0aef69b7cdfc787f822ecd7cdc76d5f1991584
Studio                  : 0.487.0
@p-kuen p-kuen added the kind/bug A reported bug. label Jul 12, 2023
@janpio janpio added 5.0.0 bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. topic: fluent api labels Jul 12, 2023
@Jolg42 Jolg42 added this to the 5.1.0 milestone Jul 12, 2023
@Jolg42 Jolg42 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jul 18, 2023
@Jolg42
Copy link
Member

Jolg42 commented Jul 19, 2023

@p-kuen and upvoters: The fix is merged and will be a part of 5.1 release.
You can try 5.1.0-dev.28 development snapshot and let us know if that version works for you. We don't recommend using it in production, but it should be enough to confirm the fix.

@p-kuen
Copy link
Author

p-kuen commented Jul 19, 2023

Thanks for the fix! We will test the dev version. The PR shows a negative impact in typescript compilation, do I read this correctly? But I think it's still better than in versions before 5.0.0, right?

@Jolg42
Copy link
Member

Jolg42 commented Jul 19, 2023

@p-kuen I guess you saw the "Codspeed" comment #20197 (comment) It is unfortunately not reliable for this, so it's hard to say if there is any actual performance impact.
I definitely recommend ignoring it and using 5.0.0 and up.

@p-kuen
Copy link
Author

p-kuen commented Jul 22, 2023

I can confirm that the Fluent API error is fixed with current dev version (5.1.0-dev.61), although I have lots of other errors with enums and other typings. We will wait until the release of 5.1.0 and look if problems still exist.
Thanks again for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0.0 bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: fluent api topic: prisma-client
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants