-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit in nested scope for hasMany relations #6832
Comments
@fabripeco thank you reporting this. I can reproduce it on my end. And you're right, it is because of the way we query related data. In here, we query all related models with all target foreign keys. i.e when it fetches related models, it has filter: {
limit:10
where:{
userId:{inq: [1, 2, ...],
deleted: false
} instead of two separate queries. The workaround I can think about atm is to manipulate the results D: |
Ok, thanks for your reply. |
I am having the same issue I think. In my schema Tenants have multiple Clients and Clients have multiple Providers. When I write a query for tenants that includes Clients and includes the Providers inside the Clients I get a max depth exceeded error. public async exportTenantData(
@param.path.number('tenantId') tenantId: number
): Promise<any> {
const inclusions = [
{
relation: 'clients',
scope: {
include: [
{
relation: 'providers',
scope: {
fields: { id: true }
}
},
]
}
},
];
const filter = { include: inclusions };
return this.tenantRepository.findById(tenantId, filter);
} |
This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the |
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the |
This should not have been closed w/o a fix. Also, This filter worked fine on LB3. |
Thanks, @achrinza |
Apologies for letting it go stale; I'm not familiar with the LB3 codebase so unfortunately I can't provide info on how it's been done historically, but I think your idea makes sense given the current architecture of the @loopback/repository package. If I'm not mistaken. this is the part of the code that needs changing: loopback-next/packages/repository/src/relations/has-many/has-many.inclusion-resolver.ts Lines 69 to 75 in cb29cbe
We can see it passes all foreign key instances into Though I think we should introduce a new filter type (i.e. |
Thanks @achrinza! Sorry I was away this weekend.
Here the first limit 10 should be applied to the source model, and the limit 5 should be applied to the related model addresses. If you still think we should introduce the parentLimit/subLimit, could you please provide an example of how will it be used? |
@collaorodrigo7 @achrinza |
Sorry for missing your message @collaorodrigo7
Apologies if it was a bit confusing; The suggestion was more in the spirit of keeping backwards compatibility, but I think it should be fine to replace the original Just that IMO we should continue to provide the original behaviour somehow or provide a way for people to emulate the current behaviour, as the bug may have become an expected "feature" for some users. I admit my original proposal was confusing and unintuitive; Maybe using The current behaviour is acting like a "global limit" for the relations, so that means In contrast, the expected behaviour should mean that |
I agree because the limit param is inside the scope of the relation. If someone really need the total number of related items differently distributed among the parent items (a bit strange, but possible) it's better to use a different param. |
Thank you all. I am working on other things at the moment, but I am gonna try to look at this soon and submit a PR. Hopefully this weekend |
If a scope filter is passed, the filter should be applied for each foreign key and not globally. This implies a query for each fk, and may impact performance. However, is the only way to respect the scope filter. Example, and include with scope.limit:2 should include 2 instances of the related model for each fk. Previously scope.limit:2 will cause to "only" include 2 related models. Signed-off-by:Rodrigo Collao <[email protected]> fix loopbackio#6832 Signed-off-by: Rodrigo Collao <[email protected]>
Sorry for the delay, the PR is ready. At the moment it is behaving the same as LB3 used to. Example of what the PR does:Say we have:
Where folders have many files through folderId. If we query:
Will return
|
While the code for
Hence, |
Oh, I see what you mean!. Thanks. [Uptade] |
If a scope filter is passed, the filter should be applied for each foreign key and not globally. This implies a query for each fk, and may impact performance. However, is the only way to respect the scope filter. Example, and include with scope.limit:2 should include 2 instances of the related model for each fk. Previously scope.limit:2 will cause to "only" include 2 related models. fix loopbackio#6832 Allow to set a totalLimit filter in scope to apply a limit globally in case someone relied on this bug thinking it was a feature. docs: add note to include with filters about totalLimit option Signed-off-by: Rodrigo Collao <[email protected]>
If a scope filter is passed, the filter should be applied for each foreign key and not globally. This implies a query for each fk, and may impact performance. However, is the only way to respect the scope filter. Example, and include with scope.limit:2 should include 2 instances of the related model for each fk. Previously scope.limit:2 will cause to "only" include 2 related models. fix #6832 Allow to set a totalLimit filter in scope to apply a limit globally in case someone relied on this bug thinking it was a feature. docs: add note to include with filters about totalLimit option Signed-off-by: Rodrigo Collao <[email protected]>
This alternative inclusion lookup implementation is currently chosen to be used in overly aggressive manner. The following scope configuration will also trigger the use of it:
As such, I would strongly recommend this ...
... to be implemented in code. Although it eludes me why order and filter are included in the explanation as those can remain global. That execution path should really be chosen only when limit is specified (and even then I'd suggest there to be a warning logged in development env about the degraded performance). |
Hi,
I'm wondering if the 'limit' param works if added to the scope of nested relations, to limit the number of items of a hasMany relation. I have a strange behaviour in the response.
I have a BusinessPartner model with hasMany relation to Addresses
I want to get the businessPartners with only the first address for each one. My filter param looks like
The where clause in the scope of the include works well, but the limit: 1 param returns the address for only one businessPartner, not one for each businessPartner. If I increase the value of the limit, e.g. 10, the response returns max 10 address, differently distribuited on the businessPartners.
It looks like the query on the related models was performed only once and not for each 'parent' instance (businessPartner).
Am I wrong in something? Is this the intended behaviour?
I'm using a postgresql connector and this is my ecosystem (Nodejs v12.16.2 npm v6.14.4 - Postresql 12)
Thank you very much
PS: maybe related to #3453
The text was updated successfully, but these errors were encountered: