How to `include` two separate references of same model using Sequelize ORM?
<p>I'm building a website using the MEAN stack but replacing MongoDB with PostGRES and, as a result, using Sequelize ORM.</p>

<p>I have two models -- <code>User</code> and <code>AudioConfig</code>. A <code>User</code> can have many <code>AudioConfig</code> and an <code>AudioConfig</code> belongs to a <code>User</code> by the <code>createdBy</code> and <code>updatedBy</code>.</p>

<p>Here's how my association looks like using Sequelize</p>

<pre><code>models.User.hasMany(models.AudioConfig, {
  foreignKey: {
    name: 'createdBy',
    allowNull: false
  }
});
models.User.hasMany(models.AudioConfig, {
  foreignKey: {
    name: 'updatedBy'
  }
});
models.AudioConfig.belongsTo(models.User, {
  foreignKey: {
    name: 'createdBy',
    as: 'createdBy',
    allowNull: false
  }
});
models.AudioConfig.belongsTo(models.User, {
  foreignKey: {
    name: 'updatedBy',
    as: 'updatedBy'
  }
});
</code></pre>

<p>In my <code>findAll</code> query for <code>AudioConfig</code>, I have tried several variations from what I've found online but none appear to work as I expect:</p>

<pre><code>var Db = require('../../models');
var entityModel = Db.AudioConfig;

exports.index = function (req, res) {
  entityModel.findAll({
      include: {all:true}
    })
    .then(function (entities) {
      return res.status(200).json(entities);
    })
    .catch(function (err) {
      return handleError(res, err);
    })
};

// And I've tried this...
exports.index = function (req, res) {
  entityModel.findAll({
      include: [
        {model: Db.User, as: 'createdBy'},
        {model: Db.User, as: 'updatedBy'}
      ]
    })
    .then(function (entities) {
      return res.status(200).json(entities);
    })
    .catch(function (err) {
      return handleError(res, err);
    })
};

// And this too...
exports.index = function (req, res) {
  entityModel.findAll({
      include: [
        {
          model: Db.User
        }
      ]
    })
    .then(function (entities) {
      return res.status(200).json(entities);
    })
    .catch(function (err) {
      return handleError(res, err);
    })
};
</code></pre>

<p>Now, in my database, I have a single record of <code>AudioConfig</code> that has two different <code>User</code> references -- one for <code>createdBy</code> and another for <code>updatedBy</code>. But when I do a query for <code>AudioConfig</code>, I only get the <code>User</code> record back for the <code>updatedBy</code> field.</p>

<pre><code>[
  {
    "id": "e3011e31-b907-47ad-99f3-61016283a523",
    "sampleRate": 16000,
    "format": "WAV",
    "channel": 2,
    "bitRate": 16,
    "createdAt": "2016-05-01T16:30:11.847Z",
    "updatedAt": "2016-05-01T16:30:11.847Z",
    "createdBy": "1375263f-a3f0-4eef-800f-99b28fdce9d8",
    "updatedBy": "5bb8cac0-b916-4000-81fe-9b1f8f597847",
    "User": {
      "id": "5bb8cac0-b916-4000-81fe-9b1f8f597847",
      "email": "johnd@email.com",
      "firstName": "John",
      "lastName": "Doe",
      "resetPasswordToken": null,
      "resetPasswordTokenExpiresOn": null,
      "createdAt": "2016-05-01T16:30:11.816Z",
      "updatedAt": "2016-05-01T16:30:11.816Z",
      "roleId": "10ae3879-9f9f-4370-aa47-3677c492afd8"
    }
  }
]
</code></pre>

<p>How do I get it so that <code>createdBy</code> value of UUID is replaced with the <code>User</code> object associated with it? And same for the <code>updatedBy</code> field?</p>

<p>I'm somewhat expecting the same behavior as with <a href="http://mongoosejs.com/docs/populate.html" rel="noreferrer">MongoDB and Mongoose's <code>populate</code></a></p>