MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / joinedload

Method joinedload

lib/sqlalchemy/orm/strategy_options.py:238–325  ·  view source on GitHub ↗

Indicate that the given attribute should be loaded using joined eager loading. This function is part of the :class:`_orm.Load` interface and supports both method-chained and standalone operation. examples:: # joined-load the "orders" collection on "User

(
        self,
        attr: _AttrType,
        innerjoin: Optional[bool] = None,
    )

Source from the content-addressed store, hash-verified

236 return cloned
237
238 def joinedload(
239 self,
240 attr: _AttrType,
241 innerjoin: Optional[bool] = None,
242 ) -> Self:
243 """Indicate that the given attribute should be loaded using joined
244 eager loading.
245
246 This function is part of the :class:`_orm.Load` interface and supports
247 both method-chained and standalone operation.
248
249 examples::
250
251 # joined-load the "orders" collection on "User"
252 select(User).options(joinedload(User.orders))
253
254 # joined-load Order.items and then Item.keywords
255 select(Order).options(joinedload(Order.items).joinedload(Item.keywords))
256
257 # lazily load Order.items, but when Items are loaded,
258 # joined-load the keywords collection
259 select(Order).options(lazyload(Order.items).joinedload(Item.keywords))
260
261 :param innerjoin: if ``True``, indicates that the joined eager load
262 should use an inner join instead of the default of left outer join::
263
264 select(Order).options(joinedload(Order.user, innerjoin=True))
265
266 In order to chain multiple eager joins together where some may be
267 OUTER and others INNER, right-nested joins are used to link them::
268
269 select(A).options(
270 joinedload(A.bs, innerjoin=False).joinedload(B.cs, innerjoin=True)
271 )
272
273 The above query, linking A.bs via "outer" join and B.cs via "inner"
274 join would render the joins as "a LEFT OUTER JOIN (b JOIN c)". When
275 using older versions of SQLite (< 3.7.16), this form of JOIN is
276 translated to use full subqueries as this syntax is otherwise not
277 directly supported.
278
279 The ``innerjoin`` flag can also be stated with the term ``"unnested"``.
280 This indicates that an INNER JOIN should be used, *unless* the join
281 is linked to a LEFT OUTER JOIN to the left, in which case it
282 will render as LEFT OUTER JOIN. For example, supposing ``A.bs``
283 is an outerjoin::
284
285 select(A).options(joinedload(A.bs).joinedload(B.cs, innerjoin="unnested"))
286
287 The above join will render as "a LEFT OUTER JOIN b LEFT OUTER JOIN c",
288 rather than as "a LEFT OUTER JOIN (b JOIN c)".
289
290 .. note:: The "unnested" flag does **not** affect the JOIN rendered
291 from a many-to-many association table, e.g. a table configured as
292 :paramref:`_orm.relationship.secondary`, to the target table; for
293 correctness of results, these joins are always INNER and are
294 therefore right-nested if linked to an OUTER join.
295

Calls 1