I am modelling a boxing tournament.
Boxers and Fights have a many-to-many relationship:
- A Boxer has many Fights
- A Fight has many Boxers (exactly 2)
Here are the models in the schema
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])
}
When creating a boxer I use the fight's name and the 2 boxer ids:
const fight = await prisma.fight.create({
data: {
name,
boxers: {
createMany: {
data: [
{
boxerId: boxerId1,
},
{
boxerId: boxerId2,
},
],
},
},
},
})
How would I update the fight if a boxer needed to be changed? Something like this? I'm not sure if I use update and set
const fight = await prisma.fight.update({
data: {
name: newName,
boxers: {
set: {
data: [
{
boxerId: newBoxerId1,
},
{
boxerId: newBoxerId2,
},
],
},
},
},
})