Skip to content
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

Nested lazy load result on wrong internal queries #1039

Open
Marian0 opened this issue Jun 23, 2024 · 2 comments
Open

Nested lazy load result on wrong internal queries #1039

Marian0 opened this issue Jun 23, 2024 · 2 comments

Comments

@Marian0
Copy link

Marian0 commented Jun 23, 2024

Package version

20.6.0

Describe the bug

Hello everyone,

I really appreciate your work on Adonis, it is definitely one of the best and more productive frameworks I've ever work with.

While running some nested lazy loading as example:

    const brevet = await Brevet.findOrFail(request.param('id'))
    await brevet.load('activity')
    await brevet.load('athlete')
    await brevet.athlete.load('club')
    await brevet.athlete.load('media')
    await brevet.activity.load('media')
    await brevet.athlete.club.load('media')
    return brevet

I see in DB debugging inspector that if media_id is null on any on the related models, the orm result on triggering those useless DB queries:

Screenshot_23_06_2024__18_15

I managed to fix this by conditionally lazy load models but it would be great to be the ORM enough smart to don't trigger those queries if the requested foreign key is null.

Temporary fix:

    const brevet = await Brevet.findOrFail(request.param('id'))
    await brevet.load('activity')
    await brevet.load('athlete')
    await brevet.athlete.load('club')
    if (brevet.athlete.mediaId) {
      await brevet.athlete.load('media')
    }
    if (brevet.activity.mediaId) {
      await brevet.activity.load('media')
    }
    if (brevet.athlete.club.mediaId) {
      await brevet.athlete.club.load('media')
    }
    return brevet

Any help would be welcomed as I'm also open to dig deeper on this if someone could guide me how to.

Thank you!

Reproduction repo

No response

@RomainLanz
Copy link
Member

Hey @Marian0!

The issue seems not to be "nested lazy load" but more about what to do if the FK is null.
I recall that this is something we already discussed many times, but I don't remember where or when. I will have to do some search and get back to you.

@Marian0
Copy link
Author

Marian0 commented Jun 27, 2024

hey @RomainLanz

Thanks for your response actually you are right, it seems to be a missing early return before hitting DB if FK is null

If you give me more insights i can try to submit a PR with a tentative solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants