Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
Prisma - How to order Users by the amount of Posts (orderBy and _count)

I found this query in the docs: It says "sort by the count of related records." which is exactly what I want.

const getActiveUsers = await prisma.user.findMany({
  take: 10,
  orderBy: {
    posts: {
      _count: 'desc',
    },
  },
})

https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting#sort-by-relation-aggregate-value

Eventhough it looks promising it is not working and gives me the following error:

{
  "errors": [
    {
      "message": "\nInvalid `prisma.user.findMany()` invocation:\n\n{\n  orderBy: {\n    posts: {\n    ~~~~~~~~~~~~~~\n      _count: 'desc'\n    }\n  }\n}\n\nUnknown arg `posts` in orderBy.posts for type UserOrderByInput. Available args:\n\ntype UserOrderByInput {\n  uid?: SortOrder\n  username?: SortOrder\n  pictureURL?: SortOrder\n}\n\n",
      "locations": [
        {
          "line": 4,
          "column": 3
        }
      ],

Here is my Prisma schema: its a simple one-to-many relation:

model User {
  uid String @id @default(uuid())
  username String? @db.VarChar(24) @unique
  pictureURL String? @db.VarChar(255)
  posts Posts[] @relation
}

What am I missing? Do I have to use keywords like "include" or "select" to get it running?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!