Is this how I should create a hierarchical data st...
# orm-help
a
Is this how I should create a hierarchical data structure so I can recursively render my comments?
Copy code
model Comment {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now()) @map("created_at")
  updatedAt DateTime  @updatedAt @map("updated_at")
  text      String
  author    User?     @relation(fields: [authorId], references: [id])
  authorId  Int?      @map("author_id")
  post      Post?     @relation(fields: [postId], references: [id])
  postId    Int?      @map("post_id")
  replies   Comment[] @relation("CommentToComment")
  parent    Comment?  @relation("CommentToComment", fields: [parentId], references: [id])
  parentId  Int?      @map("parent_id")

  @@map("comments")
}
1
I'm hoping something like this would be possible on the front end.

https://i.postimg.cc/V6st1vkv/Screen-Shot-2021-01-19-at-1-43-55-AM.png

n
Hey Adam 👋 yes, this looks good to me! You probably already saw this, but these kinds of self-referential relationships are also documented here 🙂
👍 1
a
Yes I did read that. I just was not confident. This is what is considered an adjacency list right?
@nikolasburk I would have to make a mutation for createComment and createReply right? I'm asking because I tried to use createComment for both and sometimes you don't have a parentId to pass as an argument because it's a top level comment and other times you do because it's a reply. But in the createComment mutation there is no way to set that behaviour. I kept ending up with an authorization error when I did something like this.
Copy code
...
parentId: {
  connect: {
    id: args.parentId ? args.parentId : null
  }
}
...