【发布时间】:2021-10-20 18:46:18
【问题描述】:
我想创建一个架构,其中实体 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?
}
有什么想法吗?
【问题讨论】:
标签: postgresql next.js one-to-many database-schema prisma