PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label children. Show all posts
Showing posts with label children. Show all posts

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)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

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
All Comments
Atom
All Comments

Copyright © PHPFixing