PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to find the count of users who doesn't have tasks in sequelize

 October 21, 2022     children, count, has-many, nested, sequelize.js     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing