【问题标题】:How to order by field called "count" in Prisma?如何在 Prisma 中按名为“count”的字段排序?
【发布时间】:2021-11-19 16:28:50
【问题描述】:

我很难找到如何通过 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
      }
    });

棱镜模型:

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 ``

附言。我在 GitHub 上发现了这个问题,它仍然是打开的 https://github.com/prisma/prisma/issues/7806,我也会在那里询问一些解决方法或什么的......

感谢您的帮助:-)

【问题讨论】:

    标签: prisma


    【解决方案1】:

    此问题已在 Prisma Version 3.0.1 中修复。

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

    示例 Prisma 架构

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-23
      • 2019-09-13
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多