What's the best way to fetch a tree? `include` onl...
# orm-help
r
What's the best way to fetch a tree?
include
only populates immediate children. Is there a recursive include or do I have to build the tree myself? What if I want to order/filter/limit the roots? I want to avoid having to do multiple queries.
Example:
Copy code
model Todo {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  text      String   // mutable
  done      Boolean  @default(false) // mutable
  parentId  Int?     // mutable; TODO not used yet
  parent    Todo?    @relation("tree", fields: [parentId], references: [id])
  children  Todo[]   @relation("tree")
}
Copy code
await db.todo.findMany({include: {children: true}})
r
@Ralf Vogler 👋 Currently we do not support recursive relationships out of the box, but do add a 👍 here so that we can set the priority. Currently you would need to manually provide the depth required or use a raw query with recursive CTE’s if using Postgres.
👍 1