I am trying to findMany() users and orderBy their post count. I found exactly how to do it in the docs and it works except it counts users with 0 posts as +infinity. orderBy: {posts: {count: asc}}
For example, the ordering works in ascending starting with users with 1 post up until the users with a lot of posts, but the last users have 0 posts. Vice versa when descending.
I am using postgres as the provider, I really don’t know how to even google for this problem, and title this question. Please help.
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
user User @relation(fields: [userID], references: [id])
userID Int
}
await prisma.user.findMany({
orderBy: {
posts: {
count: 'desc',
},
},
});
Output of a small test in descending order. You can see the post count is ordered 0 2 1 (in ascending order its output order is 1 2 0):
[
{ id: 3, _count: { posts: 0 } },
{ id: 1, _count: { posts: 2 } },
{ id: 2, _count: { posts: 1 } }
]