0
0
NestJSframework~10 mins

Relations in Prisma in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a one-to-many relation in Prisma schema.

NestJS
model User {
  id    Int     @id @default(autoincrement())
  posts Post[] [1]
}

model Post {
  id     Int  @id @default(autoincrement())
  userId Int
  user   User @relation(fields: [userId], references: [id])
}
Drag options to blanks, or click blank then click option'
A@unique
B@relation(fields: [userId], references: [id])
C@relation("UserPosts")
D@default(0)
Attempts:
3 left
💡 Hint
Common Mistakes
Using @unique on the array field instead of @relation
Omitting the @relation decorator on the array side
Using @default on relation fields
2fill in blank
medium

Complete the code to fetch a user with their posts using Prisma Client.

NestJS
const userWithPosts = await prisma.user.findUnique({
  where: { id: userId },
  [1]: { posts: true }
});
Drag options to blanks, or click blank then click option'
Aselect
Binclude
Cwith
Drelations
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' instead of 'include' to fetch relations
Using non-existent options like 'with' or 'relations'
3fill in blank
hard

Fix the error in the Prisma schema relation definition.

NestJS
model Profile {
  id     Int  @id @default(autoincrement())
  userId Int  @unique
  user   User [1]
}

model User {
  id      Int      @id @default(autoincrement())
  profile Profile?
}
Drag options to blanks, or click blank then click option'
A@relation(fields: [userId], references: [id])
B@unique
C@default(0)
D@id
Attempts:
3 left
💡 Hint
Common Mistakes
Using @unique alone without @relation
Missing fields or references in the relation decorator
4fill in blank
hard

Fill both blanks to define a many-to-many relation in Prisma schema.

NestJS
model Student {
  id       Int       @id @default(autoincrement())
  courses  Course[]  [1]
}

model Course {
  id        Int        @id @default(autoincrement())
  students  Student[]  [2]
}
Drag options to blanks, or click blank then click option'
A@relation("StudentCourses")
B@relation(fields: [studentId], references: [id])
C@relation("StudentCourses", references: [id])
D@unique
Attempts:
3 left
💡 Hint
Common Mistakes
Using different relation names on each side
Adding fields and references on the array side in many-to-many
5fill in blank
hard

Fill all three blanks to create a nested create operation with relations in Prisma Client.

NestJS
const newUser = await prisma.user.create({
  data: {
    name: 'Alice',
    posts: {
      [1]: [
        { title: 'First Post', content: 'Hello' },
        { title: 'Second Post', content: 'World' }
      ]
    },
    profile: {
      [2]: {
        bio: 'Developer'
      }
    },
    [3]: true
  }
});
Drag options to blanks, or click blank then click option'
Acreate
Cinclude
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' instead of 'include' to fetch relations
Omitting 'create' for nested related records