我的关系是这样的:

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的includeselect API,但我无法解决这个问题.有人知道这件事吗?

推荐答案

您可以使用_count子句,它允许您得到与预期类似的响应.

问题是:

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  await prisma.battle.create({
    data: {
      name: 'Battle of the Vowels',
      slug: 'battle-of-the-vowels',
      fighters: {
        create: {
          name: 'Kabal',
          description:
            'Kabal is a fictional character in the Star Wars franchise. He is a member of the Jedi Order.',
          image:
            'https://vignette.wikia.nocookie.net/starwars/images/7/7e/Kabal_HS-SWE.png/revision/latest?cb=20170504075154',
          votes: {
            create: {
              battleId: 1,
            },
          },
        },
      },
    },
  });

  //
  // Updated Query
  //
  const battle = await prisma.battle.findMany({
    // take: count,
    // skip: skip,
    include: {
      fighters: {
        include: {
          _count: {
            select: {
              votes: true,
            },
          },
        },
      },
    },
  });

  console.log(JSON.stringify(battle, null, 2));
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

以下是回复示例:

[
  {
    "id": 1,
    "slug": "battle-of-the-vowels",
    "name": "Battle of the Vowels",
    "fighters": [
      {
        "id": 1,
        "name": "Kabal",
        "image": "https://vignette.wikia.nocookie.net/starwars/images/7/7e/Kabal_HS-SWE.png/revision/latest?cb=20170504075154",
        "description": "Kabal is a fictional character in the Star Wars franchise. He is a member of the Jedi Order.",
        "_count": {
          "votes": 1
        }
      }
    ]
  }
]

使用_count子句的参考:_count prisma

Javascript相关问答推荐

如何使用CSS和JavaScript创建粘性、凝聚力的形状到形状(容器)变形?

使用typeof运算符获取对象值类型-接收字符串而不是数组

在JS中获取名字和姓氏的首字母

Klaro与Angular的集成

zoom svg以适应圆

我应该绑定不影响状态的函数吗?'

如何在不创建新键的情况下动态更改 map 中的项目?

更改JSON中使用AJAX返回的图像的路径

如何在Vue 3中创建自定义 Select 组件,并将选项作为HTML而不是props 传递?

是什么导致了这种奇怪的水平间距错误(?)当通过JavaScript将列表项元素追加到无序列表时,是否在按钮之间?

为什么我的按钮没有从&q;1更改为&q;X&q;?

MongoDB受困于太多的数据

在渲染turbo流之后滚动到元素

如何使用抽屉屏幕及其子屏幕/组件的上下文?

MongoDB中的嵌套搜索

使用CEPRESS截取时,cy.Wait()在等待5000ms的第一个路由请求时超时

Angel Auth Guard-用户只有在未登录时才能访问登录页面,只有在登录时才能访问其他页面

TabNavigator和StackNavigator之间的Reaction Native中的导航问题

脚本语法错误只是一个字符串,而不是一个对象?

JavaScript -如何跳过某个字符(S)来打乱字符串中的字符