title: Sequelize many-to-many self association
body: <p>I am trying to create a model <code>Users</code> with many-to-many association to itself to allow users to follow another users. In one query I want to retrieve the <code>Users</code> followed by the current user; in another query I want to retrieve the people that follows the current user.</p>
  
  <p>This is my <code>Users</code> model:</p>
  
  <pre><code>module.exports = (sequelize, Sequelize) =&gt; {
      const Users = sequelize.define(
          'Users',
          {
              id: {
                  type: Sequelize.INTEGER,
                  autoIncrement: true,
                  primaryKey: true,
              },
  
              name: {
                  type: Sequelize.STRING,
              },
          },
      );
      Users.associate = function(models) {
          Users.belongsToMany(Users, { as: 'following', through: models.UsersUsers });
      };
  
      return Users;
  };
  </code></pre>
  
  <p>I declare <code>UsersUsers</code>, just in case I need to add any field there:</p>
  
  <pre><code>module.exports = (sequelize, Sequelize) =&gt; {
      const UsersUsers = sequelize.define(
          'UsersUsers',
          {}
      );
      UsersUsers.associate = function(models) {};
  
      return UsersUsers;
  };
  </code></pre>
  
  <p>Then I query <code>Users</code> as:</p>
  
  <pre><code>models.Users.findOne({
      where: {
          id: req.params.id,
      },
      include: [
          {
              model: models.Users,
              as: 'following',
          },
      ],
  })
      .then((results) =&gt; {
          return res.send({
              User: results,
          });
      })
      .catch((error) =&gt; {
          return res.send(String(error));
      });
  </code></pre>
  
  <p>And I get this result:</p>
  
  <pre><code>{
      "User": {
          "id": 1,
          "name": "User1",
          "following": [
              {
                  "id": 2,
                  "name": "User2",
                  "UsersUsers": {
                      "UserId": 1,
                      "followingId": 2
                  }
              },
              {
                  "id": 3,
                  "name": "User3",
                  "UsersUsers": {
                      "UserId": 1,
                      "followingId": 3
                  }
              },
              {
                  "id": 4,
                  "name": "User4",
                  "UsersUsers": {
                      "UserId": 1,
                      "followingId": 4
                  }
              }
          ]
      }
  }
  </code></pre>
  
  <p>Now the questions: </p>
  
  <ol>
  <li><p>In my current query, how do I exclude "UsersUsers" from the result? <code>attributes: { exclude: ['UsersUsers'] }</code> did not work…</p></li>
  <li><p>How do I create a query to retrieve the current user with the users that follows him instead of the users followed by him?</p></li>
  </ol>
  
  <p>Thanks!</p>
  
  <p>--
  EDIT:</p>
  
  <p>The solution for the question 1. is to add <code>through: { attributes: [] }</code> to the included model:</p>
  
  <pre><code>models.Users.findOne({
      where: {
          id: req.params.id,
      },
      include: [
          {
              model: models.Users,
              as: 'following',
              through: {
                  attributes: [],
              },
          },
      ],
  })
      .then((results) =&gt; {
          return res.send({
              User: results,
          });
      })
      .catch((error) =&gt; {
          return res.send(String(error));
      });
  </code></pre>
  
  <p>Still pending question 2!</p>