不要怂,就是干,撸起袖子干!

Commit 4cc7dc8a by DC Committed by Sushant

fix(model): handle virtual attributes in includes (#10785)

1 parent 93e64a9a
...@@ -584,10 +584,9 @@ class Model { ...@@ -584,10 +584,9 @@ class Model {
if (include.attributes && !options.raw) { if (include.attributes && !options.raw) {
include.model._expandAttributes(include); include.model._expandAttributes(include);
// Need to make sure virtuals are mapped before setting originalAttributes include.originalAttributes = this._injectDependentVirtualAttributes(include.attributes);
include = Utils.mapFinderOptions(include, include.model);
include.originalAttributes = include.attributes.slice(0); include = Utils.mapFinderOptions(include, include.model);
if (include.attributes.length) { if (include.attributes.length) {
_.each(include.model.primaryKeys, (attr, key) => { _.each(include.model.primaryKeys, (attr, key) => {
...@@ -699,7 +698,7 @@ class Model { ...@@ -699,7 +698,7 @@ class Model {
// Validate child includes // Validate child includes
if (include.hasOwnProperty('include')) { if (include.hasOwnProperty('include')) {
this._validateIncludedElements.call(include.model, include, tableNames, options); this._validateIncludedElements.call(include.model, include, tableNames);
} }
return include; return include;
......
...@@ -40,6 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -40,6 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Project = this.sequelize.define('project', {}); this.Project = this.sequelize.define('project', {});
this.Task.belongsTo(this.User); this.Task.belongsTo(this.User);
this.User.hasMany(this.Task);
this.Project.belongsToMany(this.User, { through: 'project_user' }); this.Project.belongsToMany(this.User, { through: 'project_user' });
this.User.belongsToMany(this.Project, { through: 'project_user' }); this.User.belongsToMany(this.Project, { through: 'project_user' });
...@@ -173,6 +174,23 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -173,6 +174,23 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(user).to.include.all.keys(['field2']); 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);
});
});
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!