title: How to use having clause on relationship with sequelize node
body: <p>I have 2 models, <code>business</code> and <code>documents</code> in a <code>1:n</code> relationship, i want to filter the <code>business</code> in two ways, </p>
  
  <ol>
  <li><code>business</code> that has <code>documents</code> where every <code>document.due_balance</code> is greater than 0</li>
  <li><code>business</code> that has <code>documents</code> where every <code>document.due_balance</code> is equals to 0</li>
  </ol>
  
  <p>I want to make something like this</p>
  
  <pre><code>select
      A.name, B.due_balance, sum(B.due_balance) as total_due_balance
  from
      business A
  inner join documents B ON A.id = B.business_id
  group by A.id
  having total_due_balance &gt; 0;
  
  select
      A.name, B.due_balance, sum(B.due_balance) as total_due_balance
  from
      business A
  inner join documents B ON A.id = B.business_id
  group by A.id
  having total_due_balance = 0;
  </code></pre>
  
  <p>these will get me what i want, the problem, is that the previus code was made with sequelize ORM, and i can't change it, something like this</p>
  
  <p>const businesses = await db.business.paginate({
      attributes: [
      ...
      ],
      where: {
      ... //bunch of where
      },
      page: parseInt(params.page, 10) || 1,
      paginate: parseInt(params.limit, 10) || 10,
  });</p>
  
  <p>here is where the problem begins, i don't know how to join the tables and use the having to filter it, i have tried addind this</p>
  
  <pre><code>let toInclude;
  if (params.contactability === 'with_balance') {
      toInclude = {
          include : [
              {
                  attributes: [
                      [db.Sequelize.fn('sum', db.Sequelize.col('due_balance')), 'total_due_balance'],
                  ],
                  model: db.document,
                  as: 'documents',
                  having: db.Sequelize.where(db.Sequelize.fn('sum', db.Sequelize.col('due_balance')), {
                      $gt: 0,
                  }),
              },
          ],
      };
  } else if(params.contactability === 'without_balance') {
      toInclude = {
          include : [
              {
                  attributes: [
                      [db.Sequelize.fn('sum', db.Sequelize.col('due_balance')), 'total_due_balance'],
                  ],
                  model: db.document,
                  as: 'documents',
                  having: db.Sequelize.where(db.Sequelize.fn('sum', db.Sequelize.col('due_balance')), {
                      $eq: 0,
                  }),
              },
          ],
      };
  } else {
      toInclude = {};
  }
  const businesses = await db.business.paginate({
      attributes: [
      ...
      ],
      where: {
      ...
      },
      ...toInclude,
      page: parseInt(params.page, 10) || 1,
      paginate: parseInt(params.limit, 10) || 10,
  });
  </code></pre>
  
  <p>but that does not work at all, how can i solve this problem?</p>