【问题标题】:Sequelize Postgres - Select fields not in groupbySequelize Postgres - 选择不在 groupby 中的字段
【发布时间】:2020-01-10 01:26:58
【问题描述】:
Offer.findAll({
  where: {
    userId: id
  },
  attributes: [
    "productDetailId",
    "id",
    "createdAt",
    "userId",
    [Sequelize.fn("MAX", Sequelize.col("offerPrice")), "offerPrice"]
  ],
  group: ["productDetailId"],
  include: [
    {
      model: ProductDetail
    }
  ]
})

我有上面的sequelize 查询,它旨在为每个ProductDetail 找到最大offerPriceoffer,并将结果按productDetailId 分组。以上在mysql 中有效,但在postgres 中引发错误。我怀疑这是因为select 语句包含不在group by 中的字段,这是postgres 不允许的,但我不确定如何更新它。

更新:

错误信息:

error": {
    "name": "SequelizeDatabaseError",
    "parent": {
      "name": "error",
      "length": 169,
      "severity": "ERROR",
      "code": "42803",
      "position": "43",
      "file": "parse_agg.c",
      "line": "1388",
      "routine": "check_ungrouped_columns_walker",

【问题讨论】:

  • 您能否分享您收到的执行此查询的错误?

标签: sql database postgresql group-by sequelize.js


【解决方案1】:

我遇到了类似的问题,并且在 select 中按所有列分组不是一种情况。

您需要使用其名称和表名按列分组,所以

...
group: ["productDetail.productDetailId"],
...

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 2020-12-19
    • 2021-11-03
    • 2011-01-09
    • 2014-04-13
    • 2016-05-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多