[Python] Joined loads across multiple tables

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    I'm stuck with joined loads over multiple models. I have a FastAPI project, and I'm using Jinja to serve some HTML pages. In said page, I need to access content joined from other tables (looks like doing the access at time of doesn't work while in the Jinja template? I keep getting greenlet errors). Because I'll definitely be getting said data, I'm doing joined loads on the properties mapping to the other models:

    statement = statement.options(
    joinedload(Item.purchases),
    joinedload(Item.purchases.receipt),
    joinedload(Item.purchases.receipt.store),
    )


    However, I get this error:

    joinedload(Item.purchases.receipt),
    ^^^^^^^^^^^^^^^^^^^^^^
    File "/app/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 474, in __getattr__
    raise AttributeError(
    AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Item.purchases has an attribute 'receipt'


    Also, given I'm joining 4 tables, I feel like I should minimize the number of columns being selected across the joins, but I don't know how I'd do that.

    Continue reading...

Compartilhe esta Página