title: Sequelize: refer a foreign key between 2 models
body: <p>I'm trying to refer a foreign key between 2 models. but I'm getting this error:</p>
  <blockquote>
  <p>throw new AssociationError(`${source.name}.belongsToMany(${target.name}) requires through option, pass either a string or a model`);</p>
  <p>AssociationError [SequelizeAssociationError]: Order.belongsToMany(User) requires through option, pass either a string or a model</p>
  </blockquote>
  <p>I check some similar questions but it didn't help.
  I started working with Sequelize today so please give an example to answer.</p>
  <p><strong>db.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const fs = require(&quot;fs&quot;);
  const path = require(&quot;path&quot;);
  const Sequelize = require(&quot;sequelize&quot;);
  const basename = path.basename(module.filename);
  const db = {};
  
  const sequelize = new Sequelize(&quot;name&quot;, &quot;user&quot;, &quot;password&quot;, {
      host: &quot;localhost&quot;,
      dialect: &quot;mysql&quot;,
      logging: false,
  });
  
  fs.readdirSync(__dirname).filter(file =&gt;
      (file.indexOf(&quot;.&quot;) !== 0) &amp;&amp;
      (file !== basename) &amp;&amp;
      (file.slice(-3) === &quot;.js&quot;))
  .forEach(file =&gt; {
      const model = sequelize.import(path.join(__dirname, file));
      db[model.name] = model;
  });
  
  Object.keys(db).forEach(modelName =&gt; {
      if (db[modelName].associate) {
          db[modelName].associate(db);
      }
  });
  
  db.sequelize = sequelize;
  db.Sequelize = Sequelize;
  
  module.exports = db;
  </code></pre>
  <p><strong>user.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const Sequelize = require(&quot;sequelize&quot;);
  
  module.exports = (sequelize, DataTypes) =&gt; {
      const User = sequelize.define(&quot;User&quot;,
          {
              id: {
                  type: Sequelize.BIGINT,
                  autoIncrement: true,
                  allowNull: false,
                  primaryKey: true,
              },
              email: Sequelize.STRING(60),
              password: Sequelize.STRING(60),
              fullName: {
                  type: Sequelize.STRING(60),
                  allowNull: true,
              },
          },
      );
  
      User.associate = function (models) {
          models.User.belongsToMany(models.Order);
      };
  
      return User;
  };
  </code></pre>
  <p><strong>order.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const Sequelize = require(&quot;sequelize&quot;);
  
  module.exports = (sequelize, DataTypes) =&gt; {
      const Order = sequelize.define(&quot;Order&quot;,
          {
              id: {
                  type: Sequelize.BIGINT,
                  autoIncrement: true,
                  allowNull: false,
                  primaryKey: true,
              },
              title: Sequelize.STRING(60),
              orderNumber: Sequelize.STRING(60),
              status: Sequelize.STRING(60),
              amount: Sequelize.STRING(60),
              trackingCode: Sequelize.STRING(60),
          },
      );
  
      Order.associate = function(models) {
          models.Order.belongsToMany(models.User);
      };
  
      return Order;
  };
  </code></pre>
  <p>when I use lowercase for define name like this:</p>
  <pre class="lang-js prettyprint-override"><code>const User = sequelize.define(&quot;user&quot;,
     {
     ...
     }
  );
  const User = sequelize.define(&quot;order&quot;,
     {
     ...
     }
  );
  </code></pre>
  <p>I get this error:</p>
  <blockquote>
  <p>TypeError: Cannot read property 'belongsToMany' of undefined</p>
  </blockquote>
  <p><strong>sequelize:</strong> 5.21.6</p>