Reputation: 1
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.
Upvotes: 0
Views: 830
Reputation: 2568
You need to specify, how relation should occur: do you want to connect
an existing record as parent
, or do you want to create
it inline. For example:
const newCategory = await this.prisma.category.create({
data: {
name: dto.name,
parent: {
connect: {
id: dto.parentId,
},
},
}
});
Upvotes: 0