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.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pantharshit00commented, Dec 24, 2021

I can confirm this bug. Thanks for the report.

2reactions
capajcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot table relationships - Microsoft Support
Sometimes Excel fails to detect relationship between tables. ... If any of the tables used in the PivotTable contain columns of non-supported data...
Read more >
Prisma 4.7.0 Release - GitClear
With relationMode = "prisma" , no foreign keys are used, so relation fields will not benefit from the index usually created by the...
Read more >
Use Multiple Connections Between Tables - - PowerBI.Tips
Use Two Relationships Between Tables. For those of you who work in supply chain management this tutorial will be right up your alley....
Read more >
Chapter 3. Data, Tables, and Database Design - O'Reilly
If your customer has two or more phone numbers, you should create distinct phone number fields in the customers table. But what if...
Read more >
Creating a parameter table - Second Edition - Packt Subscription
We will use this on the parameter form so that it has the heading as Defaults . ... Related Table Cardinality, ZeroOne, The...
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