Postgres:Sequelize Group,在引用 include table 列时不起作用

Postgres: Sequelize Group, isn't working when referencing the include table column

这是我的代码, 我真的不知道如何解决这个问题了

当我输入此代码时。

     db.models.Price.findAll({
        attributes:['id', 'item.item_name'],
        group: [
            ['masterfile_price.id','item.id','item.item_name']
        ],
        // [sequelize.literal('Item.item_name')]
        //  ['masterfile_price.id']

        raw: true,
        include: [{
            model: db.models.Item,
            as: 'item',
            attributes: ['id',"item_name"],
        }],

    })
        

它会给我这个错误 错误 DatabaseError [SequelizeDatabaseError]: column "item.item_name" must appear in the GROUP BY clause or be used in an aggregate function 不知道为什么

查询是这样的

SELECT
"masterfile_price"."id",
"item"."item_name",
"item"."id" AS "item.id",
"item"."item_name" AS "item.item_name" 
FROM
"masterfile_prices" AS "masterfile_price"
LEFT OUTER JOIN "masterfile_items" AS "item" ON "masterfile_price"."itemId" = "item"."id" 
GROUP BY
"masterfile_price"."id";
    

问题是 我只想要 item_name

在我的原始查询中没有使用 sequelize 它正在工作

    SELECT
    "item"."item_name" AS "item.item_name" 
    FROM
    "masterfile_prices" AS "masterfile_price"
    LEFT OUTER JOIN "masterfile_items" AS "item" ON "masterfile_price"."itemId" = "item"."id" 
    GROUP BY
    item.item_name;

这将是

的结果
item.item_name
test9
test10

不知道为什么不能复制,续集里, 还是我遗漏了什么?

或者是sequelize group by

有问题

group 选项应该是 string | Fn | Colstring | Fn | Col 的数组。所以你需要删除额外的括号:

group: ['masterfile_price.id','item.id','item.item_name']

如果你想 select 并仅按 item.item_name 分组,那么你需要在 attributesgroup 选项中仅指明它:

db.models.Price.findAll({
        attributes:[[Sequelize.col('item.item_name'), 'item_name']],
        group: Sequelize.col('item.item_name'),
        raw: true,
        include: [{
            model: db.models.Item,
            as: 'item',
            attributes: [],
        }],
    })