P
Prisma6d ago
Radu

Does prisma make a query even though the value is null?

For example I have the following table that has connection to many model FileJob { id String @id originalName String? createdAt DateTime @default(now()) // One-to-one relations to JobContent jobContent JobContent? @relation("jobContent", fields: [jobContentId], references: [id]) jobContentId String? @unique @db.Uuid cvFile JobApplication? @relation("cvFile", fields: [cvFileId], references: [id]) cvFileId String? @unique @db.Uuid letterFile JobApplication? @relation("letterFile", fields: [letterFileId], references: [id]) letterFileId String? @unique @db.Uuid // One-to-one relation to Job job Job? @relation("job", fields: [jobId], references: [id]) jobId String? @unique @db.Uuid } All are null except one. If I have the following query: const file = await prisma.fileJob.findUnique({ where: { id: key }, include: { job: { include: { organization: true } }, jobContent: { include: { job: { include: { organization: true } } } }, cvFile: { include: { user: true, job: { include: { organization: true } } } }, letterFile: { include: { user: true, job: { include: { organization: true } } } }, }, }); Will prisma make a query for each one even though it knows that value is null? So I can safely do this and only the valid one will be queried? (for example if I have only a referecne to job and all others are null, only one query is done to get the organization?
7 Replies
Prisma AI Help
Skipping coffee for instant knowledge! Let's brew that AI answer now. Yes, you can safely include all the relations in your Prisma query, even if most of them are null. Prisma will only fetch related records for non-null foreign keys. If a relation field (like jobContentId, cvFileId, etc.) is null, Prisma will not attempt to query the related table for that relation—no unnecessary query is made for null references. This behavior is consistent with how Prisma handles nullable relations: when you use include on a relation that is null, the result for that field will simply be null, and no join or subquery is performed for that relation. You can see this pattern in the official documentation and community discussions, where including nullable relations does not result in extra queries or errors—only the valid, non-null relations are fetched and included in the result [How to query nullable fields?]. So, in your example, if only jobId is set and all other relation IDs are null, Prisma will only fetch the job (and its organization), and the other included relations will be null in the result. This is the intended and efficient behavior. If you need more details on how null and undefined are handled in queries, see the official docs: Null and undefined.
Radu
RaduOP6d ago
And if I change the direction, so That job, jobContent etc keeps the foreignKey, I can expect the same behaviour? @Prisma AI Help And if I change the direction, so That job, jobContent etc keeps the foreignKey, I can expect the same behaviour?
Prisma AI Help
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Radu
RaduOP5d ago
I have looked over the references provided but they don't really answer my question. Now I don't know if the AI is right or just making the infromation form the refrences but they talk about something else.
unrok4009
unrok40095d ago
@Radu add log: ["query"] to the PrismaClient constructor and check it by yourself
Radu
RaduOP5d ago
oh wow thanks
Nurul
Nurul5d ago
Yep, log: ["query"] should give you the exact SQL query that got generated.

Did you find this page helpful?