SQLAlchemy: Does with_entities preload the selected objects and columns?

Viewed 17

I have an SQL alchemy query in a dataloader.

query = (
  db.session.query(Thing1)
  .join(Thing2)
  .outerjoin(Thing2.possible_related_object_1)
  .outerjoin(Thing2.possible_related_object_1)
  .with_entities(
    Thing1.id,
    Thing2,
    PossibleRelatedObject1.name,
    PossibleRelatedObject2.name,
  )
)

My question: I only need the name fields from the possible related objects, if they exist. Does the fact that I am using with_entities here ensure that those columns on PossibleRelatedObject1 and PossibleRelatedObject2 get preloaded?

Because this is in a dataloader, I am trying to avoid any lazy loading. Normally, the strategy in this repo I'm working in has been to use joinedload to ensure eager loading, but in this case I don't see how to use joinedload because the possible related objects don't have a direct relationship to the query root. And my current strategy above seems pretty fast.

0 Answers
Related