首页 > 解决方案 > Prisma:1-N 关系中的过滤和计数

问题描述

model DataProvider {
  id              Int
  name            String
  applications    Application[]
  @@map("data_providers")
}

model Application {
  id                   Int
  name                 String
  data_provider_id     Int
  running              Boolean
  data_providers       DataProvider 
  @@map("applications")
}

我想为每个 dataProvider 获取running = true有多少个应用程序。

DataProvider                Application
-----------------           -----------------------------------------------
id     | name               id     | name    | data_provider_id  | running
-----------------           -----------------------------------------------
1      | dp1                2      | app2    | 1                 | true                  
                            -----------------------------------------------
                            3      | app3    | 1                 | false 

相应地达到这个结果

dataProvider = {id: 1, name: 'dp1', _count: {applications: 1}}

我试过查询

this.prisma.dataProvider.findMany({
    include: {
        _count: {
            select: {
                applications: true
            }
        }
    },
    where: {
        applications: {
            some: {
                running: {
                    equals:  true
                }
            }
        }
    },
})

我想我总是得到{applications: 2}2 to 的结果some,但我只能使用some,everynone。我错了什么?

标签: javascriptnode.jsprismaprisma2

解决方案


推荐阅读