如何使用prisma中的where子句对关系执行计数?



我有以下查询,它给出了所有帖子和所有评论的计数。现在,我想获得所有评论的计数,这些评论与已批准的字段设置为true的帖子。我似乎想不明白这个问题。

prisma.post.findMany({
include: {
_count: { select: { Comment: true } },
},
});

谢谢你的帮助。

4.3.0起可用

在模式文件中启用:

generator client {
provider        = "prisma-client-js"
previewFeatures = ["filteredRelationCount"] << add this 
}

然后查询:

await prisma.post.findMany({
select: {
_count: {
select: {
comment: { where: { approved: true } },
},
},
},
})

您需要使用Raw Query来实现这一点,因为_count上的关系过滤器尚不支持。

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

(编辑:14 - 11 - 2022)

Prisma从4.3.0版开始增加了对filteredRelationCount的支持

最新更新