Search code examples
many-to-manyprismaprisma2

How to update a many to many relationship in Prisma?


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,
          },
        ],
      },
    },
  },
})

Solution

  • Here you go an example how to do that:

    const { PrismaClient } = require('@prisma/client')
    const prisma = new PrismaClient()
    
    const saveData = async () => {
      const boxer1 = await prisma.boxer.create({
        data: {
          name: 'Boxer1',
        },
      })
    
      const boxer2 = await prisma.boxer.create({
        data: {
          name: 'Boxer2',
        },
      })
      const fight = await prisma.fight.create({
        data: {
          name: 'Fight 1',
          boxers: {
            createMany: {
              data: [
                { boxerId: boxer1.id },
                { boxerId: boxer2.id },
              ]
            },
          }
        },
        select: {
          id: true,
          name: true,
          boxers: {
            select: {
              boxer: {
                select: {
                  name: true,
                }
              }
            }
          }
        }
      })
    
      console.log(JSON.stringify(fight, null, 2))
    
      const boxer3 = await prisma.boxer.create({
        data: {
          name: 'Boxer3',
        },
      })
      
      const fightUpdated = await prisma.fight.update({
        where: {
          id: fight.id
        },
        data: {
          boxers: {
            createMany: {
              data: [
                { boxerId: boxer3.id },
              ]
            },
            deleteMany: {
              OR: [
                { boxerId: { equals: boxer1.id } },
              ]
            }
          }
        },
        select: {
          name: true,
          boxers: {
            select: {
              boxer: {
                select: {
                  name: true,
                }
              }
            }
          }
        }
      })
    
      console.log(JSON.stringify(fightUpdated, null, 2))
    
    }
    
    saveData()
    

    In the update you have to remove the previous boxer and the new one :)