When rejectOnNotFound is used, chaining deeper into a related table still throws if it doesn't find anything
See original GitHub issueBug 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:
- Created 2 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can confirm this bug. Thanks for the report.
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:I hope it’s 2. because I really think this is not a good default as it disallows a bunch of usecases.