Bird
0
0

Given the Prisma models below, what will be the result of the query prisma.user.findUnique({ where: { id: 1 }, include: { posts: true } }) if user with id 1 has two posts?

medium📝 component behavior Q13 of 15
NestJS - Database with Prisma
Given the Prisma models below, what will be the result of the query prisma.user.findUnique({ where: { id: 1 }, include: { posts: true } }) if user with id 1 has two posts?

model User {
  id    Int    @id @default(autoincrement())
  name  String
  posts Post[]
}

model Post {
  id      Int   @id @default(autoincrement())
  title   String
  userId  Int
  user    User  @relation(fields: [userId], references: [id])
}
AAn error because 'include' is not valid here
BAn object with user data but 'posts' will be undefined
CAn object with user data and an array of two post objects under 'posts'
DAn array of posts only without user data
Step-by-Step Solution
Solution:
  1. Step 1: Understand the Prisma query with include

    The query fetches a user by id and includes related posts as an array.
  2. Step 2: Check relation and data

    User with id 1 has two posts, so 'posts' will be an array of two post objects inside the user object.
  3. Final Answer:

    User object with posts array of two posts -> Option C
  4. Quick Check:

    Include fetches related data as array [OK]
Quick Trick: Include fetches related records as array or object [OK]
Common Mistakes:
  • Expecting posts to be undefined without include
  • Thinking include causes error
  • Expecting only posts without user

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes