如何与现有记录建立新的多对多关系?
How to create a new many to many relationship with existing records?
我有一个多对多的关系建模拳击比赛:
- 一场战斗可以有多个战士(总是 2 个)
- 一名战士可以参加多场战斗
我的架构如下所示:
model Fight {
id Int @id @default(autoincrement())
fighters FighterFights[]
}
model Fighter {
id Int @id @default(autoincrement())
name String @unique
fights FighterFights[]
}
model FighterFights {
fighter Fighter @relation(fields: [fighterId], references: [id])
fighterId Int
fight Fight @relation(fields: [fightId], references: [id])
fightId Int
@@id([fighterId, fightId])
}
我想用 2 名现有战士创建一场新战斗。这是我试过的:
const result = await prisma.fight.create({
data: {
fighters: {
connect: [{ id: fighter1 }, { id: fighter2 }],
},
},
})
但是我收到这个打字稿错误:
Type '{ id: any; }' is not assignable to type 'FighterFightsWhereUniqueInput'.
Object literal may only specify known properties, and 'id' does not exist in type 'FighterFightsWhereUniqueInput'.
我的 prisma.fight.create
函数调用应该是什么样子的?
您可以按照以下方式进行操作:
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const saveData = async () => {
const fighter1 = await prisma.fighter.create({
data: {
name: 'Ryu',
},
})
const fighter2 = await prisma.fighter.create({
data: {
name: 'Ken',
},
})
console.log(JSON.stringify(fighter1, null, 2));
console.log(JSON.stringify(fighter2, null, 2));
const fight = await prisma.fight.create({
data: {
fighters: {
createMany: {
data: [
{
fighterId: fighter1.id,
},
{
fighterId: fighter2.id,
},
]
},
},
},
select: {
id: true,
fighters: {
select: {
fighter: true,
},
},
},
});
console.log(JSON.stringify(fight, null, 2));
}
saveData()
它将return以下
我有一个多对多的关系建模拳击比赛:
- 一场战斗可以有多个战士(总是 2 个)
- 一名战士可以参加多场战斗
我的架构如下所示:
model Fight {
id Int @id @default(autoincrement())
fighters FighterFights[]
}
model Fighter {
id Int @id @default(autoincrement())
name String @unique
fights FighterFights[]
}
model FighterFights {
fighter Fighter @relation(fields: [fighterId], references: [id])
fighterId Int
fight Fight @relation(fields: [fightId], references: [id])
fightId Int
@@id([fighterId, fightId])
}
我想用 2 名现有战士创建一场新战斗。这是我试过的:
const result = await prisma.fight.create({
data: {
fighters: {
connect: [{ id: fighter1 }, { id: fighter2 }],
},
},
})
但是我收到这个打字稿错误:
Type '{ id: any; }' is not assignable to type 'FighterFightsWhereUniqueInput'.
Object literal may only specify known properties, and 'id' does not exist in type 'FighterFightsWhereUniqueInput'.
我的 prisma.fight.create
函数调用应该是什么样子的?
您可以按照以下方式进行操作:
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const saveData = async () => {
const fighter1 = await prisma.fighter.create({
data: {
name: 'Ryu',
},
})
const fighter2 = await prisma.fighter.create({
data: {
name: 'Ken',
},
})
console.log(JSON.stringify(fighter1, null, 2));
console.log(JSON.stringify(fighter2, null, 2));
const fight = await prisma.fight.create({
data: {
fighters: {
createMany: {
data: [
{
fighterId: fighter1.id,
},
{
fighterId: fighter2.id,
},
]
},
},
},
select: {
id: true,
fighters: {
select: {
fighter: true,
},
},
},
});
console.log(JSON.stringify(fight, null, 2));
}
saveData()
它将return以下