diff --git a/lib/associations/has-many.js b/lib/associations/has-many.js index bdd0db026be1..335b164a3e22 100644 --- a/lib/associations/has-many.js +++ b/lib/associations/has-many.js @@ -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}); diff --git a/test/integration/associations/has-many.test.js b/test/integration/associations/has-many.test.js index 902381aa1b6b..18ff9a6fb5d3 100644 --- a/test/integration/associations/has-many.test.js +++ b/test/integration/associations/has-many.test.js @@ -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', {