Challenge - 5 Problems
Prisma Relations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Prisma query with relations?
Given the Prisma schema with User and Post models where User has many Posts, what will this query return?
NestJS
const userWithPosts = await prisma.user.findUnique({ where: { id: 1 }, include: { posts: true } }); console.log(userWithPosts);
Attempts:
2 left
💡 Hint
Think about what the 'include' option does in Prisma queries.
✗ Incorrect
The 'include' option tells Prisma to fetch related records. Here, it fetches all posts related to the user with id 1 and adds them under the 'posts' key.
❓ state_output
intermediate2:00remaining
What is the value of 'post.author' after this Prisma query?
Given the models Post and User where Post has a relation to User as 'author', what does this code output?
NestJS
const post = await prisma.post.findUnique({ where: { id: 5 }, include: { author: true } }); console.log(post.author);
Attempts:
2 left
💡 Hint
Check how 'include' works with nested relations.
✗ Incorrect
Including 'author: true' fetches the related user who wrote the post. So 'post.author' contains that user's data.
📝 Syntax
advanced2:30remaining
Which option correctly defines a one-to-many relation in Prisma schema?
You want to define a User who has many Posts. Which schema snippet is correct?
Attempts:
2 left
💡 Hint
Look for the correct use of @relation with fields and references.
✗ Incorrect
Option B correctly defines the one-to-many relation with the foreign key 'userId' in Post and the relation decorator linking it to User's 'id'.
🔧 Debug
advanced2:30remaining
Why does this Prisma query throw an error?
Given the models User and Post where User has a relation to Post as 'posts', this code throws an error. What is the cause?
NestJS
const users = await prisma.user.findMany({ where: { posts: { id: 1 } } });
Attempts:
2 left
💡 Hint
Check how to filter by relations that are arrays in Prisma.
✗ Incorrect
When filtering by a relation that is an array, you must use 'some', 'every', or 'none' to specify the condition. Direct nested object causes error.
🧠 Conceptual
expert3:00remaining
How does Prisma handle cascading deletes in relations by default?
Consider a one-to-many relation where deleting a User should delete all their Posts. What is Prisma's default behavior if no cascade is specified?
Attempts:
2 left
💡 Hint
Think about database foreign key constraints and Prisma's default relation behavior.
✗ Incorrect
By default, Prisma does not cascade deletes. It enforces foreign key constraints, so deleting a User with related Posts causes an error unless cascade is explicitly set.