All quizzesMedium
Relations & Migrations — Series 2
Preview — 3 of 10 questions
How do you define a one-to-one relation between User and Profile in Prisma?
javascript
model User {
id Int @id @default(autoincrement())
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
userId Int @unique
user User @relation(fields: [userId], references: [id])
}AThe foreign key side (Profile.userId) must be marked @unique — this is what distinguishes a one-to-one from a one-to-many relation in Prisma; without @unique on userId, this would instead be a one-to-many relation
BOne-to-one relations require both models to have @relation(fields:...) simultaneously
CPrisma has a dedicated @oneToOne decorator that must be added to both sides
DOne-to-one relations are not supported in Prisma; they must be modeled as one-to-many with an application-level uniqueness check
How do you model an Employee that optionally reports to another Employee (manager) in Prisma?
javascript
model Employee {
id Int @id @default(autoincrement())
name String
managerId Int?
manager Employee? @relation("EmployeeManager", fields: [managerId], references: [id])
reports Employee[] @relation("EmployeeManager")
}ASelf-relations are not supported; Employee must reference a separate Manager model instead
BA named relation ("EmployeeManager" here) is required on a self-relation to distinguish the two different roles (manager side and reports side) the same model plays in the relationship — Prisma cannot infer which side is which without the name
CmanagerId must be a String type; self-relations don't support Int foreign keys
DThe relation name is purely cosmetic and can be omitted entirely for self-relations
What's the difference between onDelete: Cascade and onDelete: SetNull on a Prisma relation?
javascript
model Post {
id Int @id @default(autoincrement())
authorId Int?
author User? @relation(fields: [authorId], references: [id], onDelete: SetNull)
}ACascade and SetNull are interchangeable synonyms in Prisma
BSetNull deletes the parent record instead of the child when triggered
CCascade deletes the related child rows automatically when the parent is deleted; SetNull instead sets the foreign key column to NULL on the child rows, keeping them but detaching them from the deleted parent — SetNull requires the foreign key field to be optional (nullable)
DonDelete actions are enforced only by Prisma Client, never by the underlying database migration
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.