如何更新 Prisma 中的多对多关系?
How to update a many to many relationship in Prisma?
我正在为一场拳击比赛建模。
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,
},
],
},
},
},
})
这是一个如何做到这一点的例子:
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()
在更新中你必须删除以前的拳击手和新的拳击手:)
我正在为一场拳击比赛建模。
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,
},
],
},
},
},
})
这是一个如何做到这一点的例子:
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()
在更新中你必须删除以前的拳击手和新的拳击手:)