-
Notifications
You must be signed in to change notification settings - Fork 51
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
Comments
I'll look into this when I have a chance. I think the issue should be reproducible without the nesting and the For now, though, |
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 |
I've just released a new version that should call |
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. |
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:
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.
The text was updated successfully, but these errors were encountered: