title: Knex: How to write ARRAY_AGG in having clause in knex?
body: <p>I want to write this query in <code>knex</code>.</p>
  
  <p>The query fetches all the student ids from the <code>students_subject</code> (joint table from <code>students</code> and <code>subjects</code>) who are enrolled to all these subject ids <code>['123', '456', '789']</code> compulsorily. </p>
  
  <p><strong>PSQL:</strong></p>
  
  <pre><code>    select student_id from students_subject
      group by id
      having ARRAY_AGG(subject_id) @&gt; array[
        '123'::uuid, 
        '456'::uuid, 
        '789'::uuid
      ]; 
  </code></pre>
  
  <p>I tried this in <code>knex</code> something like this where <code>subject_ids</code> is an <code>array</code> of <code>subject ids</code>.  The <code>queryBuilder</code> is <code>knex</code> instance passed to this function which will be returned. </p>
  
  <p><strong>Javascript function:</strong>  (returns the knex query)</p>
  
  <pre><code>    const knex = require('../../../db/knex');
  
      const getSudents = function( queryBuilder, subject_ids) {
        return queryBuilder.innerJoin('students_subject',
            'students_subject.student_id',
            'students.id')
          .groupBy('students_subject.student_id')
          .having(knex.raw('ARRAY_AGG(students_subject.subject_id) @&gt; UNNEST(ARRAY[?]::uuid[])', [subject_ids]));
      }
  
      module.exports = getSudents;
  </code></pre>
  
  <ol>
  <li><p><code>queryBuilder</code> which is passed has other logics behind and I am not allowed to modify it. My job is to build the <code>query</code> to get the student <code>ids</code> and pass. </p></li>
  <li><p>The error I am getting is:</p></li>
  </ol>
  
  <blockquote>
    <p>error: set-returning functions are not allowed in HAVING</p>
  </blockquote>
  
  <p>Can you help me to write the correct <code>knex</code> query? </p>