Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit a573d39a
authored
Dec 03, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
fix(includes): better support for limit + required include (but without where)
2 parents
1a81cee9
c0a86d3b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
21 deletions
lib/dialects/abstract/query-generator.js
test/include/findAndCountAll.test.js
lib/dialects/abstract/query-generator.js
View file @
a573d39
...
@@ -1062,7 +1062,6 @@ module.exports = (function() {
...
@@ -1062,7 +1062,6 @@ module.exports = (function() {
// Filter statement
// Filter statement
// Used by both join and subquery where
// Used by both join and subquery where
if
(
subQuery
&&
!
include
.
subQuery
&&
include
.
parent
.
subQuery
&&
(
include
.
hasParentRequired
||
include
.
hasParentWhere
||
include
.
parent
.
hasIncludeRequired
||
include
.
parent
.
hasIncludeWhere
))
{
if
(
subQuery
&&
!
include
.
subQuery
&&
include
.
parent
.
subQuery
&&
(
include
.
hasParentRequired
||
include
.
hasParentWhere
||
include
.
parent
.
hasIncludeRequired
||
include
.
parent
.
hasIncludeWhere
))
{
joinOn
=
self
.
quoteIdentifier
(
tableLeft
+
'.'
+
attrLeft
);
joinOn
=
self
.
quoteIdentifier
(
tableLeft
+
'.'
+
attrLeft
);
}
else
{
}
else
{
...
@@ -1082,26 +1081,27 @@ module.exports = (function() {
...
@@ -1082,26 +1081,27 @@ module.exports = (function() {
if
(
include
.
where
)
{
if
(
include
.
where
)
{
joinOn
+=
' AND '
+
self
.
getWhereConditions
(
include
.
where
,
self
.
sequelize
.
literal
(
self
.
quoteIdentifier
(
as
)),
include
.
model
,
whereOptions
);
joinOn
+=
' AND '
+
self
.
getWhereConditions
(
include
.
where
,
self
.
sequelize
.
literal
(
self
.
quoteIdentifier
(
as
)),
include
.
model
,
whereOptions
);
}
// If its a multi association we need to add a where query to the main where (executed in the subquery)
// If its a multi association and the main query is a subquery (because of limit) we need to filter based on this association in a subquery
if
(
subQuery
&&
association
.
isMultiAssociation
&&
include
.
required
)
{
if
(
subQuery
&&
association
.
isMultiAssociation
&&
include
.
required
)
{
if
(
!
options
.
where
)
options
.
where
=
{};
if
(
!
options
.
where
)
options
.
where
=
{};
// Creating the as-is where for the subQuery, checks that the required association exists
// Creating the as-is where for the subQuery, checks that the required association exists
var
$query
=
self
.
selectQuery
(
include
.
model
.
getTableName
(),
{
var
$query
=
self
.
selectQuery
(
include
.
model
.
getTableName
(),
{
tableAs
:
as
,
tableAs
:
as
,
attributes
:
[
attrRight
],
attributes
:
[
attrRight
],
where
:
self
.
sequelize
.
asIs
([
joinOn
]),
where
:
self
.
sequelize
.
asIs
([
joinOn
]),
limit
:
1
limit
:
1
},
include
.
model
);
},
include
.
model
);
options
.
where
[
'__'
+
as
]
=
self
.
sequelize
.
asIs
([
options
.
where
[
'__'
+
as
]
=
self
.
sequelize
.
asIs
([
'('
,
'('
,
$query
.
replace
(
/
\;
$/
,
""
),
$query
.
replace
(
/
\;
$/
,
""
),
')'
,
')'
,
'IS NOT NULL'
'IS NOT NULL'
].
join
(
' '
));
].
join
(
' '
));
}
}
}
// Generate join SQL
// Generate join SQL
joinQueryItem
+=
joinType
+
self
.
quoteTable
(
table
,
as
)
+
' ON '
+
joinOn
;
joinQueryItem
+=
joinType
+
self
.
quoteTable
(
table
,
as
)
+
' ON '
+
joinOn
;
}
}
...
...
test/include/findAndCountAll.test.js
View file @
a573d39
...
@@ -176,5 +176,41 @@ describe(Support.getTestDialectTeaser("Include"), function () {
...
@@ -176,5 +176,41 @@ describe(Support.getTestDialectTeaser("Include"), function () {
expect
(
result
.
count
).
to
.
equal
(
1
);
expect
(
result
.
count
).
to
.
equal
(
1
);
});
});
});
});
})
})
it
(
"should return the correct count and rows when using a required belongsTo and a limit"
,
function
()
{
var
s
=
this
.
sequelize
,
Foo
=
s
.
define
(
'Foo'
,
{})
,
Bar
=
s
.
define
(
'Bar'
,
{});
Foo
.
hasMany
(
Bar
);
Bar
.
belongsTo
(
Foo
);
return
s
.
sync
({
force
:
true
}).
then
(
function
()
{
// Make five instances of Foo
return
Foo
.
bulkCreate
([{
id
:
1
},
{
id
:
2
},
{
id
:
3
},
{
id
:
4
},
{
id
:
5
}]);
}).
then
(
function
()
{
// Make four instances of Bar, related to the last four instances of Foo
return
Bar
.
bulkCreate
([{
'FooId'
:
2
},
{
'FooId'
:
3
},
{
'FooId'
:
4
},
{
'FooId'
:
5
}]);
}).
then
(
function
()
{
// Query for the first two instances of Foo which have related Bars
return
Foo
.
findAndCountAll
({
include
:
[{
model
:
Bar
,
required
:
true
}],
limit
:
2
}).
tap
(
function
()
{
return
Foo
.
findAll
({
include
:
[{
model
:
Bar
,
required
:
true
}],
limit
:
2
}).
then
(
function
(
items
)
{
expect
(
items
.
length
).
to
.
equal
(
2
);
});
});
}).
then
(
function
(
result
)
{
expect
(
result
.
count
).
to
.
equal
(
4
);
// The first two of those should be returned due to the limit (Foo
// instances 2 and 3)
expect
(
result
.
rows
.
length
).
to
.
equal
(
2
);
});
});
});
});
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment