Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a multiple-level eager load in SQLAlchemy?

I have many Cs in a B and many Bs in an A, and I have backref relationships defined. What I want to do is something like:

a = A.query().options(subqueryload(A.b).subsubqueryload(B.c)

How should this be done?

like image 679
wuxiekeji Avatar asked Feb 27 '14 14:02

wuxiekeji


People also ask

What is contains_ eager?

Use contains_eager() to load back references proactively. This prevents a lazy load to refer back to the parent object, which in turn prevents exceptions when you try to do this when the object is detached from its session.

What is Joinedload Sqlalchemy?

Joined Load This emits a LEFT OUTER JOIN. Lead object as well as the related object or collection is loaded in one step. from sqlalchemy. orm import joinedload c1 = session. query(Customer).

What is lazy loading in Sqlalchemy?

The loading of relationships falls into three categories; lazy loading, eager loading, and no loading. Lazy loading refers to objects are returned from a query without the related objects loaded at first.

What is Backref in Sqlalchemy?

The sqlalchemy backref is one of the type keywords and it passed as the separate argument parameters which has to be used in the ORM mapping objects. It mainly includes the event listener on the configuration attributes with both directions of the user datas through explicitly handling the database relationships.


1 Answers

Straight from the documentation of subqueryload:

qry = query(A).options(subqueryload(A.b).subqueryload(B.c))
like image 123
van Avatar answered Oct 29 '22 17:10

van