Home > OS >  How do I model Pages that can have an unlimited amount of subpages in Prisma
How do I model Pages that can have an unlimited amount of subpages in Prisma

Time:09-22

So I want to create a Sidebar similar to Notion where you can make a Page with a relationship where it can have multiple subpages. I have no idea where even to start.

This is what I want enter image description here

So far in my code, I have a model that looks like this.

enter image description here

I don't understand how can I make an unlimited about of pages related to pages

CodePudding user response:

Sounds like you need a one-to-many relationship to the same object. You can do that, by adding an optional parentId and then defining the relationship:

model Page {
  id         String  @id
  title      String
  // ...
  parentId   String?
  parentPage Page?   @relation(fields: [parentId], references: [id], name: "pageHierarchy")
  childPages Page[]  @relation(name: "pageHierarchy")
}
  • Related