title: sequelize count associated table rows
body: <p>Using sequelize and mySQL, I have two tables: <code>User</code> and <code>Post</code>.</p>
  <p>Relation between two tables is <code>M : N</code></p>
  <pre><code>db.User.belongsToMany(db.Post, { through: &quot;Likes&quot;, as: &quot;Liked&quot; });
  db.Post.belongsToMany(db.User, { through: &quot;Likes&quot;, as: &quot;Likers&quot; });
  </code></pre>
  <p>What I want is getting post with whole likers id and count of whole likers.</p>
  <p>I know <code>how to get whole likers</code> like this.</p>
  <pre><code>const post = await Post.findOne({
    where: { id: postId },
    attributes: [&quot;id&quot;, &quot;title&quot;, &quot;imageUrl&quot;],
    include: [{
      model: User,
      as: &quot;Likers&quot;,
      attributes: [&quot;id&quot;],
      through: { attributes: [] },
    }]
  })
  
  // result
  {
    &quot;id&quot;: 36,
    &quot;title&quot;: &quot;test&quot;,
    &quot;imageUrl&quot;: &quot;하늘이_1644886996449.jpg&quot;,
    &quot;Likers&quot;: [
      {
        &quot;id&quot;: 13
      },
      {
        &quot;id&quot;: 16
      }
    ]
  }
  </code></pre>
  <p>And, I also know <code>how to get count of whole likers</code>.</p>
  <pre><code>const post = await Post.findOne({
    where: { id: postId },
    attributes: [&quot;id&quot;, &quot;title&quot;, &quot;imageUrl&quot;],
    include: [{
      model: User,
      as: &quot;Likers&quot;,
      attributes: [[sequelize.fn(&quot;COUNT&quot;, &quot;id&quot;), &quot;likersCount&quot;]],
    }]
  })
  
  // result
  {
    &quot;id&quot;: 36,
    &quot;title&quot;: &quot;test&quot;,
    &quot;imageUrl&quot;: &quot;하늘이_1644886996449.jpg&quot;,
    &quot;Likers&quot;: [
      {
        &quot;likersCount&quot;: 2
      }
    ]
  }
  
  </code></pre>
  <p>But, I don't know how to get both of them at once.
  Check the result when I use both of them.</p>
  <pre><code>{
    model: User,
    as: &quot;Likers&quot;,
    attributes: [&quot;id&quot;, [sequelize.fn(&quot;COUNT&quot;, &quot;id&quot;), &quot;likersCount&quot;]],
    through: { attributes: [] },
  }
  
  // result
  &quot;Likers&quot;: [
    {
      &quot;id&quot;: 13,
      &quot;likersCount&quot;: 2
    }
  ]
  </code></pre>
  <p>It only shows 1 liker(id: 13)
  It must show another liker(id: 16).</p>
  <p>What is the problem?</p>