Skip to content

Commit

Permalink
Merge pull request #9 from mliszewski/fix_nested_includes
Browse files Browse the repository at this point in the history
clear out options.groupedLimit if no limit is passed on nested include
  • Loading branch information
mliszewski committed Jan 11, 2016
2 parents 8427412 + 67e2a5b commit 55b4460
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/associations/has-many.js
Expand Up @@ -295,6 +295,7 @@ HasMany.prototype.get = function(instances, options) {
where[association.foreignKey] = {
$in: values
};
delete options.groupedLimit;
}
} else {
where[association.foreignKey] = instance.get(association.source.primaryKeyAttribute, {raw: true});
Expand Down
94 changes: 94 additions & 0 deletions test/integration/associations/has-many.test.js
Expand Up @@ -116,6 +116,100 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
});
});

it('should fetch multiple layers of associations with limit and order with separate=true', function () {
var User = this.sequelize.define('User', {})
, Task = this.sequelize.define('Task', {
title: DataTypes.STRING
})
, SubTask = this.sequelize.define('SubTask', {
title: DataTypes.STRING
});

User.Tasks = User.hasMany(Task, {as: 'tasks'});
Task.SubTasks = Task.hasMany(SubTask, {as: 'subtasks'});

return this.sequelize.sync({force: true}).then(function() {
return Promise.join(
User.create({
tasks: [
{title: 'b', subtasks: [
{title:'c'},
{title:'a'}
]},
{title: 'd'},
{title: 'c', subtasks: [
{title:'b'},
{title:'a'},
{title:'c'}
]},
{title: 'a', subtasks: [
{title:'c'},
{title:'a'},
{title:'b'}
]}
]
}, {
include: [{association: User.Tasks, include: [Task.SubTasks]}]
}),
User.create({
tasks: [
{title: 'a', subtasks: [
{title:'b'},
{title:'a'},
{title:'c'}
]},
{title: 'c', subtasks: [
{title:'a'}
]},
{title: 'b', subtasks: [
{title:'a'},
{title:'b'}
]}
]
}, {
include: [{association: User.Tasks, include: [Task.SubTasks]}]
})
);
}).then(function(users) {
return User.findAll(
{
include: [{
association: User.Tasks,
limit: 2,
order: [['title', 'ASC']],
separate: true,
as: 'tasks',
include: [{association: Task.SubTasks, order: [['title', 'DESC']], separate: true, as: 'subtasks'}]
}]
}).then(function(users) {
expect(users[0].tasks.length).to.equal(2);

expect(users[0].tasks[0].title).to.equal('a');
expect(users[0].tasks[0].subtasks.length).to.equal(3);
expect(users[0].tasks[0].subtasks[0].title).to.equal('c');
expect(users[0].tasks[0].subtasks[1].title).to.equal('b');
expect(users[0].tasks[0].subtasks[2].title).to.equal('a');

expect(users[0].tasks[1].title).to.equal('b');
expect(users[0].tasks[1].subtasks.length).to.equal(2);
expect(users[0].tasks[1].subtasks[0].title).to.equal('c');
expect(users[0].tasks[1].subtasks[1].title).to.equal('a');

expect(users[1].tasks.length).to.equal(2);
expect(users[1].tasks[0].title).to.equal('a');
expect(users[1].tasks[0].subtasks.length).to.equal(3);
expect(users[1].tasks[0].subtasks[0].title).to.equal('c');
expect(users[1].tasks[0].subtasks[1].title).to.equal('b');
expect(users[1].tasks[0].subtasks[2].title).to.equal('a');

expect(users[1].tasks[1].title).to.equal('b');
expect(users[1].tasks[1].subtasks.length).to.equal(2);
expect(users[1].tasks[1].subtasks[0].title).to.equal('b');
expect(users[1].tasks[1].subtasks[1].title).to.equal('a');
});
});
});

it('should fetch associations for multiple instances with limit and order and a belongsTo relation', function () {
var User = this.sequelize.define('User', {})
, Task = this.sequelize.define('Task', {
Expand Down

0 comments on commit 55b4460

Please sign in to comment.