2
model Comment {
  id Int @id @default(autoincrement())

  content String @db.MediumText

  replyId  Int?
  reply    Comment?  @relation(name: "ReplyComments", fields: [replyId], references: [id],onDelete: NoAction, onUpdate: NoAction)
  comments Comment[] @relation(name: "ReplyComments")
  user     User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId   String
  post    Post     @relation(fields: [postId], references: [id], onDelete: Cascade)
  postId  Int

  hearts CommentHeart[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([userId])
  @@index([postId])
  @@index([replyId])
}

It works well in creating or editing a comment and a reply. but if I delete a parent comment including a reply, it wouldn't delete the parent comment before removing the reply. So, how do I do?? I mean I would like to work if I delete a parent comment, the comment will delete with any reply using 'on delete: Cascade'.

So, my solution is before deleting a parent comment, I send a query removing children replies. So, I don't like to send two queries. Just I want to send only one query.

  • You can use a recursive solution to delete the child comments before deleting the parent comment, here's a reference from a related question: https://github.com/prisma/prisma/discussions/17746 – Nurul Sundarani Mar 01 '23 at 14:48
  • Oh my god : ) Thank you so much. I really want to find it. – SeongBaek CHO Mar 01 '23 at 15:39

0 Answers0