I'm using the count method of mysql in sequelize but when I have multiple nested includes the count attribute makes the request returns less results. The problem is the same with the sum method.
I tried with the attributes group and duplicate but none of them resolved the problem.
I'm using Sequelize 4.33.4 but I tried with Sequelize 5 and the result is still the same.
Model :
// User
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
userId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
// ...
}, {
tableName: 'user'
});
User.associate = function (models) {
models.User.hasMany(models.Proposition, {foreignKey: 'userId'});
models.User.belongsToMany(models.Proposition, {as: 'Fan', through: 'like', foreignKey: 'userId'});
};
return User;
};
// Proposition
module.exports = function(sequelize, DataTypes) {
const Proposition = sequelize.define('Proposition', {
propositionId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
photo: DataTypes.STRING(150),
}, {
tableName: 'proposition'
});
Proposition.associate = function (models) {
models.Proposition.belongsTo(models.Challenge, {foreignKey: 'challengeId'});
models.Proposition.belongsTo(models.User, {foreignKey: 'userId'});
models.Proposition.belongsToMany(models.User, {as: 'Fan', through: 'like', foreignKey: 'propositionId'});
};
return Proposition;
};
// Challenge
module.exports = function(sequelize, DataTypes) {
const Challenge = sequelize.define('Challenge', {
challengeId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: DataTypes.STRING(100),
startDate: DataTypes.DATE,
endDate: DataTypes.DATE,
image: DataTypes.STRING(150),
description: DataTypes.STRING(256),
}, {
tableName: 'challenge'
});
Challenge.associate = function (models) {
models.Challenge.hasMany(models.Proposition, {foreignKey: 'challengeId'});
};
return Challenge;
};
Controller :
exports.getCurrentChallenge = function (req, res) {
logger.debug('Start getCurrentChallenge');
models.Challenge.findOne({
where: {
startDate: {
[Op.lte]: new Date()
},
endDate: {
[Op.gte]: new Date()
}
},
include: [{
model: models.Proposition,
include: [{
model: models.User,
attributes: ['userId', 'firstname', 'lastname', 'profilePicture']
}, {
model: models.User,
as: 'Fan',
attributes: []
}],
required: false,
attributes: [
'propositionId',
[models.sequelize.literal("CONCAT('"+ propositionsPicturesPath + "', `Propositions`.`photo`)"), 'photo'],
[models.sequelize.literal('COUNT(`Propositions->Fan->like`.`userId`)'), 'likes'], // Show less results
[models.sequelize.literal('IFNULL(SUM(`Propositions->Fan->like`.`userId` = ' + req.decoded.userId + '), 0)'), 'liked'] // Show less results
],
group: '`Propositions->Fan->like`.`propositionId`',
}]
}).then(function (challenge) {
return res.status(200).send(challenge);
}, function (err) {
logger.debug(err.name, err.message);
return res.sendStatus(500);
});
};
The request only returns the first proposition (the one with the lowest propositionId) when I have the 2 attributes. When I remove them it sends me all the results.