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

Commit da715a16 by Sorin Neacsu

Queries with includes with separate:true now add the source id to the attributes if it was missing

1 parent 673a59bd
...@@ -610,6 +610,10 @@ validateIncludedElement = function(include, tableNames, options) { ...@@ -610,6 +610,10 @@ validateIncludedElement = function(include, tableNames, options) {
include.duplicating = false; include.duplicating = false;
} }
if (include.separate === true && options.attributes && options.attributes.length && !_.includes(options.attributes, association.source.primaryKeyAttribute)) {
options.attributes.push(association.source.primaryKeyAttribute);
}
// Validate child includes // Validate child includes
if (include.hasOwnProperty('include')) { if (include.hasOwnProperty('include')) {
validateIncludedElements.call(include.model, include, tableNames, options); validateIncludedElements.call(include.model, include, tableNames, options);
......
...@@ -6,6 +6,7 @@ var chai = require('chai') ...@@ -6,6 +6,7 @@ var chai = require('chai')
, sinon = require('sinon') , sinon = require('sinon')
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, Sequelize = require(__dirname + '/../../../index') , Sequelize = require(__dirname + '/../../../index')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, current = Support.sequelize , current = Support.sequelize
, Promise = Sequelize.Promise , Promise = Sequelize.Promise
, _ = require('lodash'); , _ = require('lodash');
...@@ -60,6 +61,44 @@ if (current.dialect.supports.groupedLimit) { ...@@ -60,6 +61,44 @@ if (current.dialect.supports.groupedLimit) {
}); });
}); });
it('should work even if the id was not included', function () {
var User = this.sequelize.define('User', {
name: DataTypes.STRING
})
, Task = this.sequelize.define('Task', {})
, sqlSpy = sinon.spy();
User.Tasks = User.hasMany(Task, {as: 'tasks'});
return this.sequelize.sync({force: true}).then(function () {
return User.create({
id: 1,
tasks: [
{},
{},
{}
]
}, {
include: [User.Tasks]
}).then(function () {
return User.findAll({
attributes: ['name'],
include: [
{association: User.Tasks, separate: true}
],
order: [
['id', 'ASC']
],
logging: sqlSpy
});
}).then(function (users) {
expect(users[0].get('tasks')).to.be.ok;
expect(users[0].get('tasks').length).to.equal(3);
expect(sqlSpy).to.have.been.calledTwice;
});
});
});
it('should not break a nested include with null values', function () { it('should not break a nested include with null values', function () {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Team = this.sequelize.define('Team', {}) , Team = this.sequelize.define('Team', {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!