Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

390
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda