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

Commit 81063c12 by Mick Hansen

Merge pull request #4375 from sorin/sorin-separate-missing-id

include with separate:true fails if id is not part of the attributes list
2 parents 673a59bd da715a16
......@@ -610,6 +610,10 @@ validateIncludedElement = function(include, tableNames, options) {
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
if (include.hasOwnProperty('include')) {
validateIncludedElements.call(include.model, include, tableNames, options);
......
......@@ -6,6 +6,7 @@ var chai = require('chai')
, sinon = require('sinon')
, Support = require(__dirname + '/../support')
, Sequelize = require(__dirname + '/../../../index')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, current = Support.sequelize
, Promise = Sequelize.Promise
, _ = require('lodash');
......@@ -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 () {
var User = this.sequelize.define('User', {})
, 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!