首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Prisma模型自引用(一对多)

Prisma模型自引用(一对多)
EN

Stack Overflow用户
提问于 2021-08-19 02:45:08
回答 1查看 459关注 0票数 2

我想创建一个模式,在该模式中,实体Chapter具有也是Chapter的子项。

它必须是一对多的关系,因为一个章节可以有多个子节点,但只能有一个父节点。

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

代码语言:javascript
运行
复制
// 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?
}

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-19 07:10:07

这就是我们要走的路。一对多关系,其中一个父项可以有多个章节:

代码语言:javascript
运行
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68841406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档