【发布时间】:2022-01-27 14:12:54
【问题描述】:
我正在模拟拳击比赛。
Boxers 和 Fights 是多对多的关系:
- 拳击手有很多战斗
- 一场战斗有很多拳击手(正好 2 个)
这是架构中的模型
model Fight {
id Int @id @default(autoincrement())
name String
boxers BoxerFights[]
}
model Boxer {
id Int @id @default(autoincrement())
name String @unique
fights BoxerFights[]
}
model BoxerFights {
boxer Boxer @relation(fields: [boxerId], references: [id])
boxerId Int
fight Fight @relation(fields: [fightId], references: [id])
fightId Int
@@id([boxerId, fightId])
}
在创建拳击手时,我使用战斗名称和 2 个拳击手 ID:
const fight = await prisma.fight.create({
data: {
name,
boxers: {
createMany: {
data: [
{
boxerId: boxerId1,
},
{
boxerId: boxerId2,
},
],
},
},
},
})
如果需要更换拳击手,我将如何更新战斗?像这样的东西?我不确定我是否使用 update 和 set
const fight = await prisma.fight.update({
data: {
name: newName,
boxers: {
set: {
data: [
{
boxerId: newBoxerId1,
},
{
boxerId: newBoxerId2,
},
],
},
},
},
})
【问题讨论】:
标签: many-to-many prisma prisma2