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',
},
},
})
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?