Schema & Basic CRUD — Series 3

Preview — 3 of 10 questions

Besides datasource db, every schema.prisma file needs a generator client block. What does it control?

javascript
generator client {
  provider = "prisma-client-js"
}
AIt defines which database engine Prisma connects to, duplicating what datasource already does
BIt only affects formatting/linting of the schema.prisma file itself
CIt's optional boilerplate with no functional effect; prisma generate works identically without it
DIt tells Prisma what to generate from the schema — most commonly the type-safe Prisma Client JavaScript/TypeScript library, but other generators exist (for other languages/tools) and can be configured with their own provider

What does @relation(fields: [authorId], references: [id]) specify on a Post model's author field?

javascript
model Post {
  id       Int    @id @default(autoincrement())
  authorId Int
  author   User   @relation(fields: [authorId], references: [id])
}
AIt renames the author field to authorId in the generated database column
BIt marks author as a computed field with no actual database representation at all
CIt's purely optional documentation; Prisma infers the relationship automatically from field names regardless
DIt declares that Post.authorId (the scalar foreign key column, fields) points to User.id (the column it references, references) — this is what tells Prisma how to actually generate the underlying FOREIGN KEY constraint and how to resolve post.author into the related User record

Given the Post/author relation above, what does prisma.post.findMany() return for the author field, with no other options passed?

ARelations are not included by default — each returned Post object only has its own scalar fields (id, authorId, and so on); to also get the related author data, the query needs prisma.post.findMany({ include: { author: true } }) explicitly
BThe full related User object is always included automatically on every query involving a model with relations
Cauthor is returned as null unless select is used, even with include
DRelations are only excluded by default for findMany(); findUnique() includes them automatically

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.