Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js INCLUDE关联内的Sequelize计数返回错误聚合函数或GROUP BY子句_Node.js_Sql Server_Sequelize.js - Fatal编程技术网

Node.js INCLUDE关联内的Sequelize计数返回错误聚合函数或GROUP BY子句

Node.js INCLUDE关联内的Sequelize计数返回错误聚合函数或GROUP BY子句,node.js,sql-server,sequelize.js,Node.js,Sql Server,Sequelize.js,我在使用SQL Server的ExpressJS中使用了Sequelize。我有两个表叫做PersonalInfo和ProductBuy,它与PersonalInfo和ProductBuy有关系 以下是我的模型: PersonalInfo.js ProductBuy.js 我想得到每个用户的ProductBuy计数。 预期结果: { "data": [ { "id": 1, "ProductBuys": [

我在使用SQL Server的ExpressJS中使用了Sequelize。我有两个表叫做PersonalInfo和ProductBuy,它与PersonalInfo和ProductBuy有关系

以下是我的模型:

PersonalInfo.js ProductBuy.js 我想得到每个用户的ProductBuy计数。 预期结果:

{
    "data": [
        {
            "id": 1,
            "ProductBuys": [
                {
                    "total_active": 3
                }
             ]
        },
        {
            "id": 2,
            "ProductBuys": [
                {
                    "total_active": 1
                }
             ]
        }
    ]
}
以下是我在控制器中的功能:

getAll: function (req, res) {
    PersonalInfo.findAll({
        attributes: ['id'],
        include: [
            {
                model: ProductBuy,
                attributes: [
                    [Sequelize.fn('COUNT', Sequelize.col('member_id')), 'total_active']
                ],
                separate:true,
                where: {
                    status: ['Aktif', 'Expired']
                },
                group:['member_id', 'id']
            }
        ],
    }).then(value => {
        responseUtil.success(res, value)
    })
}
但是,这就是我得到的

{
    "data": [
        {
            "id": 1,
            "ProductBuys": [
                {
                    "total_active": 1
                },
                {
                    "total_active": 1
                },
                {
                    "total_active": 1
                }
             ]
        },
        {
            "id": 2,
            "ProductBuys": [
                {
                    "total_active": 1
                }
             ]
        }
    ]
}
我认为这是因为我按
member\u id
id
对它进行分组,但是如果我删除
id
,就会导致这样的错误

列“ProductBuy.id”在选择列表中无效,因为它未包含在聚合函数或GROUP BY子句中

我怎样才能做到这一点。谢谢

getAll: function (req, res) {
    PersonalInfo.findAll({
        attributes: ['id'],
        include: [
            {
                model: ProductBuy,
                attributes: [
                    [Sequelize.fn('COUNT', Sequelize.col('member_id')), 'total_active']
                ],
                separate:true,
                where: {
                    status: ['Aktif', 'Expired']
                },
                group:['member_id', 'id']
            }
        ],
    }).then(value => {
        responseUtil.success(res, value)
    })
}
{
    "data": [
        {
            "id": 1,
            "ProductBuys": [
                {
                    "total_active": 1
                },
                {
                    "total_active": 1
                },
                {
                    "total_active": 1
                }
             ]
        },
        {
            "id": 2,
            "ProductBuys": [
                {
                    "total_active": 1
                }
             ]
        }
    ]
}