I want to model nested comments, like on reddit. I'm using a one-to-many self relation like this:
model Comment {
[...]
parentId String?
parent Comment? @relation("ParentChildren", fields: [parentId], references: [id])
children Comment[] @relation("ParentChildren")
}
So each child is connected to its parent by parentId
. The comments can be infinitely nested, parents can have grand-children, grand-grand-children, and so on.
My question is - is it possible to retrieve all of the parent comment's descendants in one query? How can I do that?
My goal is to get a json object that looks kinda like this:
{
comment: 'Lorem ipsum...',
children: [
{
comment: 'Lorem ipsum...',
children: [
{
comment: 'Lorem ipsum...',
children: []
},
{
comment: 'Lorem ipsum...',
children: []
},
],
},
],
},
rawQuery
. There's an issue about it in the Prisma repo. I would suggest commenting your use-case over there so we can track the demand for this feature. – Speculate