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

Commit bdd547d0 by Mick Hansen

Merge pull request #5190 from mliszewski/master

Fix issue with nested separate includes
2 parents 5383fa39 55b4460d
...@@ -295,6 +295,7 @@ HasMany.prototype.get = function(instances, options) { ...@@ -295,6 +295,7 @@ HasMany.prototype.get = function(instances, options) {
where[association.foreignKey] = { where[association.foreignKey] = {
$in: values $in: values
}; };
delete options.groupedLimit;
} }
} else { } else {
where[association.foreignKey] = instance.get(association.source.primaryKeyAttribute, {raw: true}); where[association.foreignKey] = instance.get(association.source.primaryKeyAttribute, {raw: true});
......
...@@ -116,6 +116,100 @@ describe(Support.getTestDialectTeaser('HasMany'), function() { ...@@ -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 () { it('should fetch associations for multiple instances with limit and order and a belongsTo relation', function () {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Task = this.sequelize.define('Task', { , Task = this.sequelize.define('Task', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!