Prisma findMany() - 按计数排序 0 计数为正无穷大

问题描述

我正在尝试 findMany() 用户并按他们的帖子数排序。我在 docs 中确切地找到了如何做到这一点,并且它的工作原理是将具有 0 个帖子的用户计为 +infinity。 orderBy: {posts: {count: asc}}

例如,从有 1 个帖子的用户开始,到有大量帖子的用户,但最后一个用户有 0 个帖子,按升序排列。下降时反之。

我使用 postgres 作为提供者,我真的不知道如何 google 解决这个问题,并为这个问题命名。请帮忙。

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

按降序输出一个小测试。您可以看到帖子计数的顺序为 0 2 1(按升序其输出顺序为 1 2 0):

[
  { id: 3,_count: { posts: 0 } },{ id: 1,_count: { posts: 2 } },{ id: 2,_count: { posts: 1 } }
]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)