As in this document of Prisma, set
can be used to override the value of a relation.
const user = await prisma.user.update({
where: { email: '[email protected]' },
data: {
posts: {
set: [{ id: 32 }, { id: 42 }],
},
},
})
But when I tried it with explicit many-to-many relation, it does not work.
model Product {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
description String?
price Decimal
sku String @unique
published Boolean @default(false)
tags ProductTag[]
}
model Tag {
id Int @id @default(autoincrement())
name String
products ProductTag[]
}
model ProductTag {
productId String
tagId Int
createdAt DateTime @default(now())
product Product @relation(fields: [productId], references: [id])
tag Tag @relation(fields: [tagId], references: [id])
@@id([productId, tagId])
}
My code to update Product
update(id: string, updateProductDto: UpdateProductDto) {
const tags = updateProductDto.tags.map((tag) => ({
productId_tagId: {
productId: id,
tagId: tag.id,
},
}));
console.log(JSON.stringify(tags));
return this.prisma.product.update({
where: { id: id },
data: {
...updateProductDto,
tags: {
set: [...tags],
},
},
});
}
I want to update the product's tag with the product's information.
How could I implement this correctly?