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

When rejectOnNotFound is used, chaining deeper into a related table still throws if it doesn't find anything #10642

Closed
capaj opened this issue Dec 10, 2021 · 5 comments · Fixed by #16162
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: rejectOnNotFound / *OrThrow
Milestone

Comments

@capaj
Copy link

capaj commented Dec 10, 2021

Bug description

rejectOnNotFound should not cause chained queries to fail if they don't find an entity.
It is best understood by looking at the reproduction example:

How to reproduce

Have a code like this:

import { PrismaClient, Prisma } from '@prisma/client'

const prismaClient = new PrismaClient()

; (async () => {
        const existingUser = await prismaClient.user.findUnique({
            where: {
                id: 1
            },
        })
        console.log('~ existingUser', existingUser)
        const user = prismaClient.user.findUnique({
            where: {
                id: 1
            },
            rejectOnNotFound: true
        })
        console.log( await user.profilePicture())
})()

Running this code using ts-node pr.ts prints out:

~ existingUser {
  id: 1,
  createdAt: 2021-12-10T11:24:30.096Z,
  email: 'john@example.com',
  name: null,
  role: 'USER',
  profilePictureId: null
}
[NotFoundError: No User found] { clientVersion: '3.6.0' }

Expected behavior

Expected output for me is this:

~ existingUser {
  id: 1,
  createdAt: 2021-12-10T11:24:30.096Z,
  email: 'john@example.com',
  name: null,
  role: 'USER',
  profilePictureId: null
}
null

Without throwing any errors.

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./bugRepro.db"
}

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?

  role             String @default("USER")
  profilePictureId Int?
  profilePicture   Image? @relation(fields: [profilePictureId], references: [id])
}

model Image {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  isHeader  Boolean  @default(false)
  url       String
  User      User[]
}

Environment & setup

  • OS: ubuntu
  • Database: any
  • Node.js version: 17

Prisma Version

prisma                  : 3.6.0
@prisma/client          : 3.6.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : dc520b92b1ebb2d28dc3161f9f82e875bd35d727
Studio                  : 0.440.0

@capaj capaj added the kind/bug A reported bug. label Dec 10, 2021
@capaj capaj changed the title When rejectOnNotFound is used, chaining deeper still throws When rejectOnNotFound is used, chaining deeper into a related table still throws if it doesn't find anything Dec 10, 2021
@capaj
Copy link
Author

capaj commented Dec 10, 2021

Someone might say that this behaviour is correct-that prisma is intentionally using rejectOnNotFound: true in chained queries. If that's the case there is still a bug-expected error in that case would be:

[NotFoundError: No Image found] { clientVersion: '3.6.0' }

If this is how it is intended, then it is a bad default IMHO, because in most cases you want the throw to only apply to a single level when chaining queries. Especially if you have a helper method which gets reused in multiple places.

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. topic: rejectOnNotFound / *OrThrow labels Dec 17, 2021
@MichalLytek
Copy link

MichalLytek commented Dec 21, 2021

I also suffer from the same issue. I find it a bug because it throws No XYZ found error where XYZ is the name of the base entity which exists in db, only the relation is missing.

@capaj
Copy link
Author

capaj commented Dec 21, 2021

There is definitely a bug, there's just no indication anywhere whether rejectOnNotFound: true should propagate down when chaining queries or not. So there are only 2 options:

  1. bug is just in the message of the error, otherwise the query behaves as designed/expected
  2. bug is also in the behaviour, so it should not propagate down into chained queries

I hope it's 2. because I really think this is not a good default as it disallows a bunch of usecases.

@pantharshit00
Copy link
Contributor

I can confirm this bug. Thanks for the report.

@pantharshit00 pantharshit00 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 Dec 24, 2021
@SevInf SevInf self-assigned this Oct 19, 2022
SevInf added a commit that referenced this issue Oct 19, 2022
@SevInf
Copy link
Contributor

SevInf commented Oct 19, 2022

Can reproduce it with *OrThrow varaints of findFirst and findUnique: #15887

@miguelff miguelff self-assigned this Nov 8, 2022
@miguelff miguelff added this to the 4.7.0 milestone Nov 8, 2022
miguelff added a commit that referenced this issue Nov 8, 2022
Co-authored-by: Sergey Tatarintsev <tatarintsev@prisma.io>

Closes #15887
Closes #10642
miguelff added a commit that referenced this issue Nov 8, 2022
Closes #15887
Closes #10642

Co-authored-by: Sergey Tatarintsev <tatarintsev@prisma.io>
miguelff added a commit that referenced this issue Nov 10, 2022
Closes #15887
Closes #10642

Co-authored-by: Sergey Tatarintsev <tatarintsev@prisma.io>
miguelff added a commit that referenced this issue Nov 10, 2022
Closes #15887
Closes #10642

Co-authored-by: Sergey Tatarintsev <tatarintsev@prisma.io>
jkomyno pushed a commit that referenced this issue Dec 21, 2022
Closes #15887
Closes #10642

Co-authored-by: Sergey Tatarintsev <tatarintsev@prisma.io>
jkomyno pushed a commit that referenced this issue Dec 21, 2022
Closes #15887
Closes #10642

Co-authored-by: Sergey Tatarintsev <tatarintsev@prisma.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: rejectOnNotFound / *OrThrow
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants