Bird
0
0

Identify the error in this Prisma schema for a one-to-many relation between Author and Book models:

medium📝 Debug Q14 of 15
NestJS - Database with Prisma
Identify the error in this Prisma schema for a one-to-many relation between Author and Book models:

model Author {
  id    Int    @id @default(autoincrement())
  name  String
  books Book
}

model Book {
  id       Int    @id @default(autoincrement())
  title    String
  authorId Int
  author   Author @relation(fields: [authorId], references: [id])
}
AAuthor model is missing a primary key
BMissing @relation on Author.books field
CauthorId should be optional in Book model
DThe 'books' field in Author should be an array (Book[]) not a single Book
Step-by-Step Solution
Solution:
  1. Step 1: Check relation type from schema

    Author to Book is one-to-many, so Author.books must be an array of Book.
  2. Step 2: Identify the mistake

    Author.books is declared as a single Book, which is incorrect for one-to-many.
  3. Final Answer:

    'books' must be Book[] array -> Option D
  4. Quick Check:

    One-to-many uses array field [OK]
Quick Trick: One-to-many needs array [] on 'many' side field [OK]
Common Mistakes:
  • Using single object instead of array for many side
  • Forgetting @relation decorator
  • Making foreign key optional incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes