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

Commit 3e69d920 by Jay Prakash

fix query which has nested include in a parent include with separate true and has-many association

1 parent c801a8a5
......@@ -1630,7 +1630,7 @@ var QueryGenerator = {
this.quoteIdentifier(fieldLeft)
].join('.');
if (options.groupedLimit || subQuery && include.parent.subQuery && !include.subQuery) {
if ((options.groupedLimit && parentIsTop) || (subQuery && include.parent.subQuery && !include.subQuery)) {
if (parentIsTop) {
// The main model attributes is not aliased to a prefix
joinOn = [
......
......@@ -102,6 +102,19 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
User.Posts = User.hasMany(Post, {foreignKey: 'userId', as: 'POSTS'});
var Comment = Support.sequelize.define('Comment', {
title: DataTypes.STRING,
postId: {
type: DataTypes.INTEGER,
field: 'post_id'
}
},
{
tableName: 'comment'
});
Post.Comments = Post.hasMany(Comment, {foreignKey: 'postId', as: 'COMMENTS'});
var include = Model.$validateIncludedElements({
include: [{
attributes: ['title'],
......@@ -139,6 +152,48 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
].join(current.dialect.supports['UNION ALL'] ?' UNION ALL ' : ' UNION ')
+') AS [user] LEFT OUTER JOIN [post] AS [POSTS] ON [user].[id] = [POSTS].[user_id];'
});
var nestedInclude = Model.$validateIncludedElements({
include: [{
attributes: ['title'],
association: User.Posts,
include: [{
attributes: ['title'],
association: Post.Comments
}]
}],
model: User
}).include;
testsql({
table: User.getTableName(),
model: User,
include: nestedInclude,
attributes: [
['id_user', 'id'],
'email',
['first_name', 'firstName'],
['last_name', 'lastName']
],
order: [
['last_name', 'ASC']
],
groupedLimit: {
limit: 3,
on: 'companyId',
values: [
1,
5
]
}
}, {
default: 'SELECT [user].*, [POSTS].[id] AS [POSTS.id], [POSTS].[title] AS [POSTS.title], [POSTS.COMMENTS].[id] AS [POSTS.COMMENTS.id], [POSTS.COMMENTS].[title] AS [POSTS.COMMENTS.title] FROM ('+
[
'(SELECT [id_user] AS [id], [email], [first_name] AS [firstName], [last_name] AS [lastName] FROM [users] AS [user] WHERE [user].[companyId] = 1 ORDER BY [user].[last_name] ASC'+sql.addLimitAndOffset({ limit: 3, order: ['last_name', 'ASC'] })+')',
'(SELECT [id_user] AS [id], [email], [first_name] AS [firstName], [last_name] AS [lastName] FROM [users] AS [user] WHERE [user].[companyId] = 5 ORDER BY [user].[last_name] ASC'+sql.addLimitAndOffset({ limit: 3, order: ['last_name', 'ASC'] })+')'
].join(current.dialect.supports['UNION ALL'] ?' UNION ALL ' : ' UNION ')
+') AS [user] LEFT OUTER JOIN [post] AS [POSTS] ON [user].[id] = [POSTS].[user_id] LEFT OUTER JOIN [comment] AS [POSTS.COMMENTS] ON [POSTS].[id] = [POSTS.COMMENTS].[post_id];'
});
})();
it('include (left outer join)', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!