Prisma 模型 self-referencing(一对多)

Prisma model self-referencing (one to many)

我想创建一个模式,其中实体 Chapter 具有 children 也是 Chapter

它必须是一对多的关系,因为一章可以有很多 children 但只有一个 parent。

我发现很难在我的 Prisma 模式中定义它。我尝试了一些方法,但总是显示错误:

// 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?
}

有什么想法吗?

这是必经之路。父级可以有多个章节的一对多关系:

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

我们在文档中也有这个:https://www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations#one-to-many-self-relations