【发布时间】:2019-02-28 23:58:22
【问题描述】:
续集版本:4.22.6, MySql版本:5.7.8 我想在查询执行中将 'hasMany' 关联(CompanyUser)计数(在 _user_count_ 的位置)
/**
* Company user associate with Company with belongsTo relation
*/
`CompanyUser.belongsTo(Company, { foreignKey: 'company_id', targetKey: 'id'});`
/**
* Company associate with Company user with hasMany relation
*/
`Company.hasMany(CompanyUser, { foreignKey: 'company_id', sourceKey: 'id'});`
`return Company.findAll({
attributes: [
'id', 'title', 'is_enabled', '_user_count_'
]
include: [
{
model: sqConn.CompanyUser,
attributes: ['id'],
},
{
model: sqConn.CompanyLogo,
attributes:['file_object'],
}
],
}).then(function(model) {
return sequelize.Promise.resolve(model);
}).catch(function(err) {
return sequelize.Promise.reject(err);
});`
带有左连接的简单 MySQL 查询可以正常工作并给出计数。
【问题讨论】:
标签: mysql associations sequelize.js