我的关系是这样的:
model Fighter {
id Int @id @default(autoincrement())
name String
image String?
description String?
battles Battle[]
votes Vote[]
}
model Vote {
Fighter Fighter @relation(fields: [fighterId], references: [id])
fighterId Int
Battle Battle @relation(fields: [battleId], references: [id])
battleId Int
count Int @default(0)
@@id([fighterId, battleId])
}
model Battle {
id Int @id @default(autoincrement())
slug String @unique
name String
fighters Fighter[]
votes Vote[]
}
一场战斗有多个战士,有一个计票模型,计算一场战斗中每个战士的选票.我想找回一场战斗,包括战斗人员,以及每名战斗人员的投票.我问:
prisma.battle.findMany({
take: count,
skip: skip,
include: {
fighters: {
include: {
votes: {
select: {
count: true
}
}
}
}
}
});
这大致解决了我的问题,因为结果是一个战士拥有一系列投票权,如下所示:
{
"id": 2,
"slug": "Random-1",
"name": "Random 1",
"fighters": [
{
"id": 3,
"name": "1 dragon",
"image": null,
"votes": [
{
"count": 3
}
]
},
{
"id": 6,
"name": "1 hero",
"image": null,
"votes": [
{
"count": 1
}
]
}
]
}
但我想说的是,为了最好,但我怀疑这是可能的:
{
"id": 6,
"name": "1 hero",
"image": null,
"votes": 1
}
直接在我的fighter object中进行计票,或者至少在fighter object中只进行一票
{
"id": 6,
"name": "1 hero",
"image": null,
"votes": {
"count": 1
}
}
我不知道我的问题是模型之间的模式问题,还是我可以用Prisma查询解决它.我试图使用Prisma的include
和select
API,但我无法解决这个问题.有人知道这件事吗?