如何在 Prisma 中按名为 "count" 的字段排序?

How to order by field called "count" in Prisma?

我很难找到如何通过 Prisma 中的 count 字段进行排序。

查询如下所示:

const where = // ...
const limit = // ...
const skip = // ...
const data = await this.prisma.translation.findMany({
      where,
      take: limit,
      skip: offset,
      orderBy: {
        count: 'asc'
        // ^ here is the issue, because count seems to be reserved for aggregations
      }
    });

Prisma 模型:

model Translation {
  id           String
  count        Int      @default(0)
}

我在这里找到了文档 https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#orderby 但它没有说明如何向 prisma 说明计数并不意味着聚合而是列名。

我遇到的错误:

Invalid `prisma.translation.findMany()` invocation:\n\n\n  Failed to validate the query: `Assertion error: Attempted conversion of non-map ParsedInputValue (Single(String(\"asc\"))) into map failed..` at ``

PS。我在 GitHub 上发现了这个问题,它仍然是开放的 https://github.com/prisma/prisma/issues/7806,也会在那里询问一些解决方法或其他东西...

感谢您的帮助:-)

这已在 Prisma Version 3.0.1 中修复。

如果您需要在 Prisma v2 中解决此问题,您可以更改 prisma 中的字段名称并使用 @map 注释指向数据库中的 count 列 table .

示例 Prisma 架构

model Translation {
  id                      Int      @id @default(autoincrement())
  transactionCount        Int      @default(0)  @map(name: "count")
}

在您的 prisma 代码中,您将使用 transactionCount 而不是 count。但是底层数据库不会有任何变化,它仍然会有名为 count.

的列

有关详细信息,请查看 using custom field names 的 Prisma 文档。