Prisma model self-referencing (one to many)

Viewed 1415

I want to create an schema where an entity Chapter has children that are also Chapter.

It has to be a relation one to many since one chapter can have many children but only one parent.

I'm finding it difficult to define it in my Prisma schema. I have tried some approaches but always show an error:

// children and parent fields
model Chapter {
  id          Int       @default(autoincrement()) @id
  // ...
  children    Chapter[] @relation("children")
  parent      Chapter?  @relation(fields: [parentId], references: [id])
  parentId    Int?
}

// children field whith @relation
model Chapter {
  id          Int       @default(autoincrement()) @id
  // ...
  children    Chapter[] @relation("children")
}

// just children as an array of Chapter
model Chapter {
  id          Int       @default(autoincrement()) @id
  // ...
  children    Chapter[]
}

// Only parent (I could work with that)
model Chapter {
  id          Int       @default(autoincrement()) @id
  // ...
  parent      Chapter?  @relation(fields: [parentId], references: [id])
  parentId    Int?
}

Any ideas?

1 Answers
Related