Hi, how to orderBy _count? I want to sort all my u...
# orm-help
a
Hi, how to orderBy _count? I want to sort all my users by the amount of posts they have. This is what I found in the docs:
Copy code
const getActiveUsers = await prisma.user.findMany({
  take: 10,
  orderBy: {
    posts: {
      _count: 'desc',
    },
  },
})
This is not working for me, I only can choose the fields inside “orderBy” that are not arrays. Am I missing something? Its a simple one-to-many relation
Copy code
Available args:\n\ntype UserOrderByInput {\n  uid?: SortOrder\n  username?: SortOrder\n  pictureURL?:
Here is my schema:
Copy code
model User {
  uid String @id @default(uuid())
  username String? @db.VarChar(24) @unique
  pictureURL String? @db.VarChar(255)
  posts Posts[] @relation
}
the docs say it is available from “2.19.0 and later”, I was using 2.26.0. I upgradet to 3.14.0 and it fixed it for me 🙂 weird…
n
Had you enabled the preview feature flag in version 2.26.0? Like this
Copy code
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["orderByRelation"]
}
If not, then it could be a reason for not working.