Repository navigation
Description
Hi,
I am seeking some assistance with the use of limit on a nested hasMany include. It simply returns an empty object even though there’s actually data that should be returned.
I’ve looked through SO and the Github issues and it seems others have had similar problems and used things like distinct: true and subQuery: false to get around it - none of which have worked for me. Lots of these issues just pass me around in circles between the same posts.
I am at a loss as to whether this is a bug or if it’s something I am doing wrong, so would be great to hear for certain. I really need this limitation on specific includes as there could be hundreds of records that could possibly be returned, even though I only need the most recent 5. I can manually fetch this data, but that means more queries and processing time, then manual processing to "connect" the data back into the right places.
What are you doing?
Here is the query that has the issue:
// const references = ['PANEL00001', 'PANEL00002', 'PANEL00003']
return LocalDB.Panels
.findAll({
where: { reference: references },
include: [
{
model: LocalDB.PanelEvents,
order: [['timestamp', 'DESC']],
limit: 3 //<- DOES NOT WORK - causes query to return [{}]
},
{
model: LocalDB.Communicators,
include: [
{
model: LocalDB.CommunicatorStatusUpdates,
order: [['timestamp', 'DESC']],
limit: 1 //<- DOES NOT WORK - causes query to return [{}]
}
]
}
],
returning: true,
logging: false
})I made a full test example, which you can get here. Switch to branch 1-limit-does-not-apply-on-nested-findall on that repository and read the readme to connect to a local PG database.
What do you expect to happen?
I want to be able to apply a limit on a nested include and it return that specific number of records.
What is actually happening?
- With the limits in place on
PanelEventsandCommunicatorStatusUpdates, all that gets returned for those specific includes is[{}] - Without the limits in place, I run up my server and database costs by requesting 00's of records when they aren't all needed.
[
{
"id": 1,
"reference": "PANEL00001",
"model": "PANEL-X",
"status": "ACTIVE",
"created_at": "2018-07-11T00:00:01.000Z",
"updated_at": "2018-07-11T00:00:01.000Z",
"Communicators": [
{
"id": 1,
"reference": "COMMUNICATO0001",
"model": "MODEL-A",
"panelReference": "PANEL00001",
"created_at": "2018-07-11T00:00:01.000Z",
"updated_at": "2018-07-11T00:00:01.000Z",
"CommunicatorStatusUpdates": [
{}
]
}
],
"PanelEvents": [
{}
]
},
{
"id": 2,
"reference": "PANEL00002",
"model": "PANEL-Y",
"status": "ACTIVE",
"created_at": "2018-07-11T00:00:01.000Z",
"updated_at": "2018-07-11T00:00:01.000Z",
"Communicators": [
{
"id": 2,
"reference": "COMMUNICATO0002",
"model": "MODEL-B",
"panelReference": "PANEL00002",
"created_at": "2018-07-11T00:00:01.000Z",
"updated_at": "2018-07-11T00:00:01.000Z",
"CommunicatorStatusUpdates": [
{}
]
}
],
"PanelEvents": [
{}
]
},
{
"id": 3,
"reference": "PANEL00003",
"model": "PANEL-Z",
"status": "ACTIVE",
"created_at": "2018-07-11T00:00:01.000Z",
"updated_at": "2018-07-11T00:00:01.000Z",
"Communicators": [
{
"id": 3,
"reference": "COMMUNICATO0003",
"model": "MODEL-C",
"panelReference": "PANEL00003",
"created_at": "2018-07-11T00:00:01.000Z",
"updated_at": "2018-07-11T00:00:01.000Z",
"CommunicatorStatusUpdates": [
{}
]
}
],
"PanelEvents": [
{}
]
}
]Dialect: postgres
Dialect version: PostgreDB 9.6.6 (node pg: 7.4.3)
Database version: PostgreDB 9.6.6 (node pg: 7.4.3)
Sequelize version: 4.38.0
Tested with latest release: 4.38.0
Reading prior to posting this issue:
#9188
#7344
#1897
#6891
https://stackoverflow.com/questions/31309369/sequelize-limit-and-order-inside-an-include-construct
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
rsispal commentedon Jul 30, 2018
18 days later...nobody?
seblemeur commentedon Jul 30, 2018
Same problem ...
dietrichg commentedon Aug 27, 2018
Same problem.
seblemeur commentedon Aug 27, 2018
@dietrichg , I found the solution by updating at 4.38.0 sequelize.
rsispal commentedon Aug 30, 2018
My example project is using 4.38.0 and there is still an issue..
darkotuning commentedon Oct 13, 2018
Same problem....
kluchy-dev commentedon Feb 4, 2019
Same issue:
This works:
But when I add offset it does not work:
It generates:
Dialect: mssql - tedious
Sequelize version: 4.42.0
Database version: MSSQL Server 2008 R2 v10.50.1600.1
stale commentedon May 5, 2019
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
rsispal commentedon May 5, 2019
8 months later and this is still an issue. Having to manually retrieve records with a limit separately which is cumbersome.
Anyone else still having this problem too?
snax4a commentedon Sep 10, 2019
I have same issue, when I do .findOne and include hasMany model it is generating only one SQL query with LEFT OUTER JOIN and it returns me source model data and target model data but when I will add limit parameter to include object it is making two queries one for the source model and one for the target model - both queries are correct and return data but sequelize gives me empty data for the included model
papb commentedon Sep 14, 2019
@rsispal Thanks for the detailed report and for providing a SSCCE. Unfortunately I don't have time to look into it right now...
Can you please post all those links you think are related? Thanks
rsispal commentedon Sep 15, 2019
All the details are available in the original post, including the cycle links.
I’ve stopped using Sequelize now because of this and how ambiguous parts of the docs are.
That said, I’m sure others above can continue to assist
papb commentedon Sep 15, 2019
Oops, you are right, sorry about that!! Thank you very much.
barrydevp commentedon Nov 21, 2019
anyone fix this issue?
rsispal commentedon Nov 21, 2019
Still not working as intended with the latest version.
barrydevp commentedon Nov 24, 2019
hey bro, what is your replacement for this?
kevingilbert100 commentedon Mar 22, 2020
Still an issue.
HevertonPires commentedon Oct 3, 2020
I still have the same problem in version 5.22.3
oaayoub commentedon May 29, 2024
this issue still have not been fixed !