use*_*947 3 postgresql one-to-many database-schema next.js prisma
我想创建一个模式,其中实体Chapter的子级也是Chapter.
它必须是一对多的关系,因为一个章节可以有多个子项,但只能有一个父项。
我发现很难在我的 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?
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
这是要走的路。父级可以有多个章节的一对多关系:
model Chapter {
id Int @id @default(autoincrement())
children Chapter[] @relation("children")
parent Chapter? @relation("children", fields: [parentId], references: [id])
parentId Int? @map("chapterId")
}
Run Code Online (Sandbox Code Playgroud)
我们在文档中也有这个: https: //www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations#one-to-many-self-relations
| 归档时间: |
|
| 查看次数: |
4513 次 |
| 最近记录: |