我使用mongodb v5.0.13
//schema.prisma
model Video {
id Int @id @map("_id")
title String
description String
tags Tag[]
}
model Tag {
id String @id @map("_id")
video Video? @relation(fields: [videoId], references: [id])
videoId Int?
}
我希望能够为视频记录添加标签。
//index.ts
await prisma.video.create({
data: {
id: 1,
description: "",
title: "Dancing Cats",
},
});
let tag = {id: "funny"};
await prisma.tag.create({
data: tag,
});
await prisma.video.update({
where: {
id: 1,
},
data: {
tags: {
push: tag,
},
},
});
我得到这个错误每当我运行update:
error TS2322: Type '{ push: Tag | null; }' is not assignable to type 'TagUncheckedUpdateManyWithoutVideoNestedInput | TagUpdateManyWithoutVideoNestedInput | undefined'.
Object literal may only specify known properties, and 'push' does not exist in type 'TagUncheckedUpdateManyWithoutVideoNestedInput | TagUpdateManyWithoutVideoNestedInput'.
38 push: tag,
~~~~~~~~~
使用mongodb驱动程序的代码没有太大不同(语法方面),它可以工作。
你有没有试过:
await prisma.video.create({
data: {
id: 1,
description: "",
title: "Dancing Cats",
},
});
let tag = {id: "funny"};
const newTag = await prisma.tag.create({
data: tag,
});
await prisma.video.update({
where: {
id: 1,
},
data: {
tags: {
push: newTag,
},
},
});
?