如何对 prisma 中的 where 子句的关系进行计数?

Chr*_*ook 7 prisma

我有以下查询,它给出了所有帖子和所有评论的计数。现在,我想获取该帖子中已批准字段设置为 true 的所有评论的计数。我似乎无法弄清楚这一点。

prisma.post.findMany({
    include: {
      _count: { select: { Comment: true } },
    },
  });
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。

eag*_*gor 11

自4.3.0起可用。

在您的架构文件中启用:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["filteredRelationCount"] << add this 
}
Run Code Online (Sandbox Code Playgroud)

然后查询:

await prisma.post.findMany({
  select: {
    _count: {
      select: {
        comment: { where: { approved: true } },
      },
    },
  },
})
Run Code Online (Sandbox Code Playgroud)


Nur*_*ani 4

您需要使用原始查询_count来实现此目的,因为尚不支持关系过滤器。

这是相同的功能请求:能够在“计数关系功能”中过滤计数

[编辑:2022 年 11 月 14 日]

自4.3.0版本以来,Prisma 添加了对 FilteredRelationCount 的支持