sqlakeyset icon indicating copy to clipboard operation
sqlakeyset copied to clipboard

Unable to eager load queries with nested one to many relationships

Open sashahilton00 opened this issue 6 months ago • 4 comments

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.

sashahilton00 avatar Aug 18 '24 02:08 sashahilton00