title: "has many through" association in Sequelize
body: <p>Let's say we have three models: </p>
  
  <ul>
  <li>books</li>
  <li>chapters</li>
  <li>paragraphs</li>
  </ul>
  
  <p>Here are their associations:</p>
  
  <ul>
  <li><strong>Books</strong> have many <strong>chapters</strong>. </li>
  <li><strong>Chapters</strong> have many <strong>paragraphs</strong>. </li>
  <li><strong>Books</strong> have many <strong>paragraphs</strong>, through <strong>chapters</strong>.</li>
  </ul>
  
  <p><strong>Is it possible to define a 'has many, through' relationship with Sequelize? If so, how?</strong></p>
  
  <p>Here are very basic models for Book, Chapter, and Paragraph:</p>
  
  <pre><code>// Book model
  const Book = sequelize.define('Book', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true
    },
    title: {
      type: DataTypes.STRING
    }
  }, {
    classMethods: {
      associate: (models) =&gt; {
        Book.hasMany(models.Chapter, {
          foreignKey: 'bookId',
          as: 'chapters'
        });
      }
      // How can you add an association for a book having many paragraphs, through chapters?
    }
  });
  
  
  // Chapter model
  const Chapter = sequelize.define('Chapter', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true
    },
    title: {
      type: DataTypes.STRING
    }
  }, {
    classMethods: {
      associate: (models) =&gt; {
        Chapter.hasMany(models.Paragraph, {
          foreignKey: 'chapterId',
          as: 'paragraphs'
        });
  
        Chapter.belongsTo(models.Book, {
          foreignKey: 'bookId'
        });
      }
    }
  });
  
  
  // Paragraph Model
  const Paragraph = sequelize.define('Paragraph', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true
    },
    content: {
      type: DataTypes.TEXT
    }
  }, {
    classMethods: {
      associate: (models) =&gt; {
        Paragraph.belongsTo(models.Chapter, {
          foreignKey: 'chapterId'
        });
      }
      // How can you add an association for paragraphs belonging to a book "through" chapters?
    }
  });
  </code></pre>