Issue
How to find the count of users who doesn't have tasks in Sequelize.Users and Tasks are different models and Users hasMany Tasks( foreign key relation )
Solution
You can proceed following way within single sequelize query
UserModel.findAndCountAll({
offset: 0,
limit: 10,
group: ["user.id"],
includeIgnoreAttributes: false,
include: [
{
model : TaskModel
}
],
attributes: [
"id",
[Sequelize.fn("COUNT", Sequelize.col("tasks.id")), "taskCount"]
],
having: Sequelize.literal(`taskCount > 0`)
})
This way you can get those users who has no tasks paginatedly and the results count
property will hold the total number of such users who has no task.
Answered By - Ratul Sharker Answer Checked By - Pedro (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.