question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Search by relation count eq | gt | lt

See original GitHub issue

Problem

I think the issue should be related with https://github.com/prisma/prisma/issues/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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:42
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
P4sca1commented, Nov 12, 2022

Please just upvote the original issue instead of spamming replies.

2reactions
ipaldcommented, Nov 14, 2022

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use the $filter query parameter - Microsoft Graph
Learn how to use the $filter OData query parameter and its operators against different types of properties in Microsoft Graph.
Read more >
Filtering and sorting (Concepts) - Prisma
Use the Prisma Client API to filter records by any combination of fields or related record fields, and sort the results.
Read more >
$match (aggregation) — MongoDB Manual
For your $search queries against data on your Atlas cluster, you can use the Atlas Search compound operator filter option to match or...
Read more >
Rails: Using greater than/less than with a where statement
I'm trying to find all Users with ...
Read more >
Queries on Rails - Active Record and Arel (2020)
Here you'll find good examples explaining how to do it. ... The predicates #eq , #not_eq , #lt , #gt , #lteq ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found