Repository navigation
Closed
Labels
topic: countFor issues and PRs. Things that involve counting such as the `count` and the `findAndCountAll` meth.type: bugDEPRECATED: replace with the "bug" issue type
Description
What are you doing?
I have two models with many to many association. Below are Client and user models which are associated through ClientUser model, I am trying to use findAndCountAll using ClientUser model, when I don't use distinct: true, I get wrong result may be as it counts all associated models but even if I do distinct: true I always get count 1 as it seems to be doing distinct in ClientUser.ClientId which is always 1 as i am trying to get all users in specific client id.
//clients model
module.exports = (sequelize, DataTypes) => {
let Client = sequelize.define('Client', {
clientId: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING(255), allowNull: false },
});
Client.associate = (models) => {
Client.belongsToMany(models.User, {
through: models.ClientUser,
as: 'users',
foreignKey: 'clientId',
otherKey: 'userId'
});
};
return Client;
}
//user model
module.exports = (sequelize, DataTypes) => {
let User = sequelize.define('User', {
userId: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
username: {
type: DataTypes.STRING(255), unique: true, validate: {
min: 3,
max: 30,
is: /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,30}$/i
}
},
password: {
type: DataTypes.STRING(255),
validate: {
min: 6
}
},
}, {
defaultScope: {
attributes: { exclude: ['password'] },
}
});
User.associate = (models) => {
User.belongsToMany(models.Client, {
through: models.ClientUser,
as: 'clients',
foreignKey: 'userId',
otherKey: 'clientId'
});
};
return User;
}
//client user
module.exports = (sequelize, DataTypes) => {
let ClientUser = sequelize.define('ClientUser', {
status: {
type: DataTypes.ENUM,
values: ['active', 'inactive']
}
});
ClientUser.associate = (models) => {
ClientUser.belongsTo(models.Client, {as: 'client', foreignKey: 'clientId'});
ClientUser.belongsTo(models.User, {as: 'user', foreignKey: 'userId'});
};
return ClientUser;
}
//query here
let params = {
include: [{
model: User,
as: 'user',
}],
where: {
clientId: 1
},
distinct: true
};
let result = await ClientUser.findAndCountAll(params);What do you expect to happen?
I expect the correct result with correct count.
What is actually happening?
produces wrong count.
without distinct: true
SELECT count(`ClientUser`.`clientId`) AS `count` FROM `ClientUsers` AS `ClientUser` LEFT OUTER JOIN `Users` AS `user` ON `ClientUser`.`userId` = `user`.`userId` LEFT OUTER JOIN `ContactInformations` AS `user->contactInformation` ON `user`.`userId` = `user->contactInformation`.`userId` LEFT OUTER JOIN `InstantMessengerInfos` AS `user->instantMessengerInfos` ON `user`.`userId` = `user->instantMessengerInfos`.`userId` LEFT OUTER JOIN `Roles` AS `role` ON `ClientUser`.`roleId` = `role`.`roleId` WHERE `ClientUser`.`clientId` = '1';with distinct: true
SELECT count(DISTINCT(`ClientUser`.`clientId`)) AS `count` FROM `ClientUsers` AS `ClientUser` LEFT OUTER JOIN `Users` AS `user` ON `ClientUser`.`userId` = `user`.`userId` LEFT OUTER JOIN `ContactInformations` AS `user->contactInformation` ON `user`.`userId` = `user->contactInformation`.`userId` LEFT OUTER JOIN `InstantMessengerInfos` AS `user->instantMessengerInfos` ON `user`.`userId` = `user->instantMessengerInfos`.`userId`
LEFT OUTER JOIN `Roles` AS `role` ON `ClientUser`.`roleId` = `role`.`roleId` WHERE `ClientUser`.`clientId` = '1';Dialect: mysql
Dialect version: 1.6.4
Database version: 5.7.24
Sequelize version: 4.41.2
Tested with latest release: Yes 4.41.2 (If yes, specify that version)
Metadata
Metadata
Assignees
Labels
topic: countFor issues and PRs. Things that involve counting such as the `count` and the `findAndCountAll` meth.type: bugDEPRECATED: replace with the "bug" issue type
Activity
leogoesger commentedon Dec 8, 2018
Seeing the same issue with count. Current work around
rmoura-92 commentedon Dec 27, 2018
same issue here.
very annoying any group by query is returning count as like
[{count: '1'}, {count: '1'}, {count: '1'}]MatthewHerbst commentedon Jan 2, 2019
Possibly related: #9481 and #9669
Alexsey commentedon Jan 16, 2019
The first issue on this #9109
stale commentedon Apr 16, 2019
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
MatthewHerbst commentedon Apr 16, 2019
This is not a stale issue, and in fact (imho) is one of Sequelize's most egregious bugs.
stale commentedon Jul 15, 2019
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
MatthewHerbst commentedon Jul 15, 2019
This is not a stale issue.
papb commentedon Jul 25, 2019
Is this an issue only with MySQL or does it happen with another dialect too?
papb commentedon Jul 30, 2019
Duplicate of #9109
faraz9x commentedon Jan 24, 2022
distinct: true works but remember, your query must include an @Index column