0
0
NestJSframework~20 mins

Relations in Prisma in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prisma Relations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
AAn array of posts without any user data
BOnly the user data without any posts included
CAn object with user data and an array of all posts by that user under the 'posts' key
DA syntax error because 'include' is not valid here
Attempts:
2 left
💡 Hint
Think about what the 'include' option does in Prisma queries.
state_output
intermediate
2: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);
AAn object containing the author's user data for the post with id 5
BUndefined because 'author' is not included by default
CAn array of all users in the database
DA runtime error because 'author' is not a valid relation
Attempts:
2 left
💡 Hint
Check how 'include' works with nested relations.
📝 Syntax
advanced
2: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?
A
model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

model Post {
  id       Int  @id @default(autoincrement())
  userId   Int
}
B
model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

model Post {
  id       Int  @id @default(autoincrement())
  userId   Int
  user     User @relation(fields: [userId], references: [id])
}
C
model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

model Post {
  id       Int  @id @default(autoincrement())
  user     User
}
D
model User {
  id    Int    @id @default(autoincrement())
  posts Post
}

model Post {
  id       Int  @id @default(autoincrement())
  userId   Int
  user     User
}
Attempts:
2 left
💡 Hint
Look for the correct use of @relation with fields and references.
🔧 Debug
advanced
2: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 } }
});
AMissing 'include' to fetch related author data
BThe 'posts' field does not exist in the User model
CThe 'findMany' method cannot filter by relations
DThe 'where' filter uses a nested object without 'some' or 'every' for a relation array
Attempts:
2 left
💡 Hint
Check how to filter by relations that are arrays in Prisma.
🧠 Conceptual
expert
3: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?
APrisma prevents deleting the User if related Posts exist, throwing an error
BPrisma automatically deletes all related Posts when the User is deleted
CPrisma sets the foreign key in Posts to null when the User is deleted
DPrisma ignores the relation and deletes the User without affecting Posts
Attempts:
2 left
💡 Hint
Think about database foreign key constraints and Prisma's default relation behavior.