Vyassa Baratham
Vyassa Baratham

Reputation: 1467

How to eagerly load all relationships in SQLAlchemy

I have the following model:

class Item(Base):
    a = relationship(<whatever>)
    b = relationship(<whatever>)
    c = relationship(<whatever>)
    d = relationship(<whatever>)
    other_stuff = Column(<whatever>)

Most of the time, I just want to see the other_stuff column, so I don't specify lazy='joined' in the relationship. But sometimes, I want to see all the joined fields, and I want them to be loaded in one SQL query. I could do the following:

query(Item).options(joinedload('a')).options(joinedload('b')).options(joinedload('c')).options(joinedload('d'))

But I feel like this is a common enough use case that there has to be a prettier way to do it.

Upvotes: 6

Views: 8841

Answers (1)

mmcclannahan
mmcclannahan

Reputation: 1748

You can simply say .options(joinedload('*')).

For reference: http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#wildcard-loading-strategies

Upvotes: 17

Related Questions