author avatar

ayasha.pandey

Fri Oct 25 2024

onDelete: Cascade in Prisma automatically deletes the child records when a parent record is deleted.




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

model Post {
  id     Int   @id @default(autoincrement())
  userId Int
  user   User  @relation(fields: [userId], references: [id], onDelete: Cascade)
}


Now when you delete a user:


await prisma.user.delete({ where: { id: 123 } })


All their posts are automatically deleted too!

#prisma #schema