question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findUnique return type does not include null

See original GitHub issue

Problem

The docs say findUnique can return zero or one items that match a query. The issue is that the Typescript return type of findUnique is simply a promise of an instance of the model you are querying and does not account for when there isn’t a match and the result is null. This can lead to bugs like Cannot read properties of null (reading 'password') unless you check the result of findUnique isn’t null.

See below where user is typed User instead of User | null.

Screen Shot 2021-12-29 at 3 44 27 PM

Suggested solution

A setting or something so that I can type the return type of findUnique to also include null to force proper type checking. Another alternative is to throw an error if the query doesn’t find a match which is how Django does it. But maybe such a setting exists and it’s not documented?

Alternatives

I know I could just remember to check the output of findUnique but the whole point of Typescript is so I don’t have to remember things like this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

10reactions
florianmartenscommented, May 24, 2022

I also strongly believe this issue should not be closed. My understanding is that prisma’s main feature is it’s advanced type safety. Here we have a function that returns null | Type but does not label itself as such. findUnique is simply not typed correctly in Prisma and rejectOnNotFound is a hatch - not a fix.

9reactions
MeoMixcommented, Jun 7, 2022

I also strongly believe this issue should not be closed. My understanding is that prisma’s main feature is it’s advanced type safety. Here we have a function that returns null | Type but does not label itself as such. findUnique is simply not typed correctly in Prisma and rejectOnNotFound is a hatch - not a fix.

+1

I want a solution where rejectOnNotFound is false and the return type of findUnique<T> is T | null

Read more comments on GitHub >

github_iconTop Results From Across the Web

findUnique query returns null for array fields - prisma
I read the Prisma Relations documentation and it fixed my findMany query which is able to return valid data but ...
Read more >
Prisma Client API (Reference)
Its return type is non-nullable. For example, post.findUnique() can return post or null , but post.findUniqueOrThrow() always returns post .
Read more >
prisma schema nullable | The AI Search Engine You Control
It's return type of find* methods and such. It's supposed to match DB table and table does not have undefined or optional rows,...
Read more >
Prisma | NestJS - A progressive Node.js framework
You can find an in-depth comparison of the type-safety guarantees of Prisma and ... UserWhereUniqueInput, ): Promise<User | null> { return this.prisma.user.
Read more >
COUNT DISTINCT and COUNT UNIQUE functions - IBM
If every column value is NULL, the COUNT DISTINCT function returns zero (0). The UNIQUE keyword has the same meaning as the DISTINCT...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found