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

Unable to eager load queries with nested one to many relationships #110

Open
sashahilton00 opened this issue Aug 18, 2024 · 4 comments
Open

Comments

@sashahilton00
Copy link

Currently it is not possible to eager load relationships on a query against an entity that has a one to many relationship using sqlalchemy 2.0 style queries.

As an example:

q = select(User).where(*query_conditions).join(User.contacts).options(contains_eager(User.contacts).contains_eager(Contact.addresses)).order_by(User.id)

current_page = await select_page(s, q, per_page=results_per_page, page=page_marker)

where User -> Contact is a one to many relationship, and Contact -> Address is also a one to many relationship, the following error is thrown:

sqlalchemy.exc.InvalidRequestError: The unique() method must be invoked on this Result, as it contains results that include joined eager loads against collections

This is expected as detailed in the tip here, but currently there is no way to call unique on the results from sqlakeyset as the handling of the results from sqlalchemy is done internally by sqlakeyset with no way to override the default handling.

@acarapetis
Copy link
Collaborator

I'll look into this when I have a chance. I think the issue should be reproducible without the nesting and the contains_eager, right? Just a simple select(User).options(joinedload(User.contacts)) should produce the same error?

For now, though, selectinload might work for you?

@sashahilton00
Copy link
Author

I may have a workaround which I'll post later when I'm back at my desk. I believe selectinload does work.

I didn't try without contains_eager - I just copied out the problematic query as an example of what triggers the behaviour.

@acarapetis
Copy link
Collaborator

I've just released a new version that should call .unique() automatically when necessary. Give it a shot with your query and let me know how it goes :)

@sashahilton00
Copy link
Author

Unfortunately the latest change doesn't seem to fix the issue. I've spent several hours wrestling with this, mostly to try and keep things relatively performant, and for anyone else looking for a workaround in future, this is what I ended up using:

q = select(User).\
                where(*query_conditions).\
                options(
                selectinload(User.contacts),
                joinedload(User.contacts, innerjoin=True).selectinload(Contact.addresses),
                # joinedload(User.contacts, innerjoin=True).noload(Contact.metadata)
            ).\
                order_by(User.id, User.created_at)

The commented line in there is just as an example of how not to load unnecessary relationships. Also please note that innerjoin has it's own set of assumptions that should be checked in the official docs before usage.

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