title: Simple example of many-to-many relation using Sequelize
body: <p>I'm trying to build a simple example of many-to-many relation between tables using Sequelize. However, this seems to be way trickier than I expected.</p>
  <p>This is the code I have currently (the <code>./db.js</code> file exports the Sequelize connection instance).</p>
  <pre><code>const Sequelize = require(&quot;sequelize&quot;);
  const sequelize = require(&quot;./db&quot;);
  
  var Mentee = sequelize.define('mentee', {
      id: {
          type: Sequelize.INTEGER,
          primaryKey: true,
          autoIncrement: true
      },
      name: {
          type: Sequelize.STRING
      }
  });
  
  var Question = sequelize.define('question', {
      id: {
          type: Sequelize.INTEGER,
          primaryKey: true,
          autoIncrement: true
      },
      text: {
          type: Sequelize.STRING
      }
  });
  
  var MenteeQuestion = sequelize.define('menteequestion', {
  //    answer: {
  //        type: Sequelize.STRING
  //    }
  });
  
  // A mentee can answer several questions
  Mentee.belongsToMany(Question, { as: &quot;Questions&quot;, through: MenteeQuestion });
  
  // And a question can be answered by several mentees
  Question.belongsToMany(Mentee, { as: &quot;Mentees&quot;, through: MenteeQuestion });
  
  let currentQuestion = null;
  Promise.all([
      Mentee.sync({ force: true })
    , Question.sync({ force: true })
    , MenteeQuestion.sync({ force: true })
  ]).then(() =&gt; {
      return Mentee.destroy({where: {}})
  }).then(() =&gt; {
      return Question.destroy({ where: {} })
  }).then(() =&gt; {
      return Question.create({
          text: &quot;What is 42?&quot;
      });
  }).then(question =&gt; {
      currentQuestion = question;
      return Mentee.create({
          name: &quot;Johnny&quot;
      })
  }).then(mentee =&gt; {
      console.log(&quot;Adding question&quot;);
      return mentee.addQuestion(currentQuestion);
  }).then(() =&gt; {
      return MenteeQuestion.findAll({
          where: {}
        , include: [Mentee]
      })
  }).then(menteeQuestions =&gt; {
      return MenteeQuestion.findAll({
          where: {
              menteeId: 1
          }
        , include: [Mentee]
      })
  }).then(menteeQuestion =&gt; {
      console.log(menteeQuestion.toJSON());
  }).catch(e =&gt; {
      console.error(e);
  });
  </code></pre>
  <p>When running this I get:</p>
  <blockquote>
  <p>Cannot add foreign key constraint</p>
  </blockquote>
  <p>I think that is because of the <code>id</code> type—however I have no idea why it appears and how we can fix it.</p>
  <p>Another error which appeared when the previous one won't appear was:</p>
  <blockquote>
  <p>Executing (default): <em>INSERT INTO <code>menteequestions</code> (<code>menteeId</code>,<code>questionId</code>,<code>createdAt</code>,<code>updatedAt</code>) VALUES (2,1,'2017-03-17 06:18:01','2017-03-17 06:18:01');</em></p>
  <p>Error: mentee is not associated to menteequestion!</p>
  </blockquote>
  <p>Also, another error I get—I think it's because of <code>force:true</code> in <code>sync</code>—is:</p>
  <blockquote>
  <p><em>DROP TABLE IF EXISTS <code>mentees</code>;</em></p>
  <p>ER_ROW_IS_REFERENCED: Cannot delete or update a parent row: a foreign key constraint fails</p>
  </blockquote>
  <p>How to solve these?</p>
  <p>Again, I only need a minimal example of many-to-many crud operations (in this case just insert and read), but this seems to be beyond my understanding. Was struggling for two days with this.</p>