Node.js INCLUDE关联内的Sequelize计数返回错误聚合函数或GROUP BY子句
我在使用SQL Server的ExpressJS中使用了Sequelize。我有两个表叫做PersonalInfo和ProductBuy,它与PersonalInfo和ProductBuy有关系 以下是我的模型: PersonalInfo.js ProductBuy.js 我想得到每个用户的ProductBuy计数。 预期结果: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": [
{
"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
}
]
}
]
}