Skip to content

Search and filter by relation count, with eq, gt, lt etc. #8935

@kunyan

Description

@kunyan

Problem

I think the issue should be related with #3821

model Topic {
  id              Int                @id @default(autoincrement())
  title           String             @db.MediumText
  content         String             @db.LongText
  voters          VotersOnTopics[]
  watchers        WatchersOnTopics[]
  viewers         ViewerOnTopics[]
}
model VotersOnTopics {
  voter   User     @relation(fields: [voterId], references: [id])
  voterId Int
  topic   Topic    @relation(fields: [topicId], references: [id])
  topicId Int
  votedAt DateTime @default(now())

  @@id([voterId, topicId])
}
model User {
  id             Int                    @id @default(autoincrement())
  createdAt      DateTime               @default(now())
  updatedAt      DateTime               @updatedAt
  username       String                 @unique
  name           String
  email          String                 @unique
  avatarUrl      String?
  comments       Comment[]
  votedTopics    VotersOnTopics[]
  watchedTopics  WatchersOnTopics[]
  viewedTopics   ViewerOnTopics[]       @relation("viewed")
}

The topic could be voted by many users

so what if I want to search by a votes condition ?

const result = await db.topic.findMany({
      where: {
        title: 'Hello world'
      },
      select: {
        _count: {
          select: {
            watchers: true,
            voters: true,
          },
        },
      },
    }
})

There are no _count could be use in where conditions

Suggested solution

const result = await db.topic.findMany({
      where: {
          voters: {
            _count: {
               gt: 40,
               lt: 100
            }
          }
      },
      select: {
        _count: {
          select: {
            watchers: true,
            voters: true,
          },
        },
      },
    }
})

Alternatives

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureA request for a new feature.topic: _counthttps://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count-topic: relationstopic: where / filter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions