title: Sequelize - How to apply Distinct to nested include?
body: <p>I am using Nodejs, Express, Postgresql, and Sequelize</p>
  
  <p>I have 4 Models</p>
  
  <ol>
  <li><p>Account (has many books)</p></li>
  <li><p>Book (has many parts)</p></li>
  <li>Part (has many chapters)</li>
  <li>Chapter</li>
  </ol>
  
  <p>I'm returning a book and including both 'Part' and 'Chapter' with some specific ordering - This works fine. </p>
  
  <p>The model 'Chapter' has a column 'topic' and I only want to include rows from the 'Chapter' model that have distinct 'topics'. </p>
  
  <p>How do I apply distinct to this 'Chapter' model with this nested include structure?</p>
  
  <p>Code snippet:</p>
  
  <pre><code>return Book
  
  .findOne({
      where: {accountid: req.body.accountid,
          id: req.body.bookid}, 
      include: [{
          model: Part,
          as: 'parts'
          include: [
              {
                  model: Chapter,
                  required: false,
                  as: 'chapters',
                  where: {
                      createdAt: {
                          $gte: moment().subtract(24,'hours').format()
                      }
                  }
              }],
      }],
      order: [
          [
              { model: Part, as: 'parts' },
              'createdAt',
              'DESC'
          ],
          [
              { model: Part, as: 'parts' },
              { model: Chapter, as: 'chapters' },
              'createdAt',
              'DESC'
          ]
      ]
  
  
  })
  .then(book =&gt; res.status(200).send(book))
  .catch(error =&gt; res.status(400).send(error.toString()));
  }
  </code></pre>