Skip to content

Commit

Permalink
fix(model): handle virtual attributes in includes (#10785)
Browse files Browse the repository at this point in the history
  • Loading branch information
danconnell authored and sushantdhiman committed Apr 17, 2019
1 parent 93e64a9 commit 4cc7dc8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/model.js
Expand Up @@ -584,10 +584,9 @@ class Model {
if (include.attributes && !options.raw) {
include.model._expandAttributes(include);

// Need to make sure virtuals are mapped before setting originalAttributes
include = Utils.mapFinderOptions(include, include.model);
include.originalAttributes = this._injectDependentVirtualAttributes(include.attributes);

include.originalAttributes = include.attributes.slice(0);
include = Utils.mapFinderOptions(include, include.model);

if (include.attributes.length) {
_.each(include.model.primaryKeys, (attr, key) => {
Expand Down Expand Up @@ -699,7 +698,7 @@ class Model {

// Validate child includes
if (include.hasOwnProperty('include')) {
this._validateIncludedElements.call(include.model, include, tableNames, options);
this._validateIncludedElements.call(include.model, include, tableNames);
}

return include;
Expand Down
18 changes: 18 additions & 0 deletions test/integration/model/attributes/types.test.js
Expand Up @@ -40,6 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Project = this.sequelize.define('project', {});

this.Task.belongsTo(this.User);
this.User.hasMany(this.Task);
this.Project.belongsToMany(this.User, { through: 'project_user' });
this.User.belongsToMany(this.Project, { through: 'project_user' });

Expand Down Expand Up @@ -173,6 +174,23 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(user).to.include.all.keys(['field2']);
});
});

it('should be able to include model with virtual attributes', function() {
return this.User.create({}).then(user => {
return user.createTask();
}).then(() => {
return this.Task.findAll({
include: [{
attributes: ['field2', 'id'],
model: this.User
}]
});
}).then(tasks => {
const user = tasks[0].user.get();

expect(user.field2).to.equal(42);
});
});
});
});
});
Expand Down

0 comments on commit 4cc7dc8

Please sign in to comment.