如何在Prisma中保存树结构数据
我尝试使用带有Prisma的Nestjs构建应用程序,我的树结构保存数据有问题,这是我的类别模型。
model Category {
id Int @id @default(autoincrement())
name String
children Category[] @relation("SubCategories")
parent Category? @relation("SubCategories", fields: [parentId], references: [id])
parentId Int?
Product Product[]
@@map("categories")
}
这是我的逻辑:
async createCategory(dto: createCategoryDto) {
const newCategory = await this.prisma.category.create({
data: {
name: dto.name,
parent: dto.parentId
}
});
return newCategory;
}
但是当我试图保存数据时,我会收到此错误:
...
[Nest] 30699 - 07/06/2022, 1:31:36 PM ERROR [ExceptionsHandler]
Invalid `this.prisma.category.create()` invocation in
/home/fadhli/SandBox/lanjo/lanjo-api/src/category/category.service.ts:26:56
23 }
24
25 async createCategory(dto: createCategoryDto) {
→ 26 const newCategory = await this.prisma.category.create({
data: {
name: 'example',
parentId: '1'
~~~~~~~~
}
})
Unknown arg `parentId` in data.parentId for type CategoryCreateInput. Did you mean `parent`? Available args:
type CategoryCreateInput {
name: String
children?: CategoryCreateNestedManyWithoutParentInput
parent?: CategoryCreateNestedOneWithoutChildrenInput
Product?: ProductCreateNestedManyWithoutCategoryInput
}
如何将树数据保存到Nestjs和Prisma中的数据库中?
谢谢。
i try to build app with nestjs with prisma, i have problem with tree structure saving the data, here is my model for categories.
model Category {
id Int @id @default(autoincrement())
name String
children Category[] @relation("SubCategories")
parent Category? @relation("SubCategories", fields: [parentId], references: [id])
parentId Int?
Product Product[]
@@map("categories")
}
this is my logic:
async createCategory(dto: createCategoryDto) {
const newCategory = await this.prisma.category.create({
data: {
name: dto.name,
parent: dto.parentId
}
});
return newCategory;
}
but when i am trying to save data, i got this error:
...
[Nest] 30699 - 07/06/2022, 1:31:36 PM ERROR [ExceptionsHandler]
Invalid `this.prisma.category.create()` invocation in
/home/fadhli/SandBox/lanjo/lanjo-api/src/category/category.service.ts:26:56
23 }
24
25 async createCategory(dto: createCategoryDto) {
→ 26 const newCategory = await this.prisma.category.create({
data: {
name: 'example',
parentId: '1'
~~~~~~~~
}
})
Unknown arg `parentId` in data.parentId for type CategoryCreateInput. Did you mean `parent`? Available args:
type CategoryCreateInput {
name: String
children?: CategoryCreateNestedManyWithoutParentInput
parent?: CategoryCreateNestedOneWithoutChildrenInput
Product?: ProductCreateNestedManyWithoutCategoryInput
}
how to save tree data into database in nestjs and prisma?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要指定,关系应如何发生:您要
连接
现有记录parent
,还是要创建>创建
IT IT IT内线。例如:You need to specify, how relation should occur: do you want to
connect
an existing record asparent
, or do you want tocreate
it inline. For example: