【问题标题】:How to update a many to many relationship in Prisma?如何更新 Prisma 中的多对多关系?
【发布时间】: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,
          },
        ],
      },
    },
  },
})

如果需要更换拳击手,我将如何更新战斗?像这样的东西?我不确定我是否使用 updateset

const fight = await prisma.fight.update({
  data: {
    name: newName,
    boxers: {
      set: {
        data: [
          {
            boxerId: newBoxerId1,
          },
          {
            boxerId: newBoxerId2,
          },
        ],
      },
    },
  },
})

【问题讨论】:

    标签: many-to-many prisma prisma2


    【解决方案1】:

    这里有一个例子:

    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()
    

    在更新中,您必须删除以前的拳击手和新的拳击手:)

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 2021-12-04
      • 2021-04-11
      • 1970-01-01
      • 2021-08-24
      • 2021-09-17
      • 2023-01-27
      • 2021-10-03
      • 2023-01-31
      相关资源
      最近更新 更多