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

Method selectinload

lib/sqlalchemy/orm/strategy_options.py:357–411  ·  view source on GitHub ↗

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

(
        self,
        attr: _AttrType,
        recursion_depth: Optional[int] = None,
    )

Source from the content-addressed store, hash-verified

355 return self._set_relationship_strategy(attr, {"lazy": "subquery"})
356
357 def selectinload(
358 self,
359 attr: _AttrType,
360 recursion_depth: Optional[int] = None,
361 ) -> Self:
362 """Indicate that the given attribute should be loaded using
363 SELECT IN eager loading.
364
365 This function is part of the :class:`_orm.Load` interface and supports
366 both method-chained and standalone operation.
367
368 examples::
369
370 # selectin-load the "orders" collection on "User"
371 select(User).options(selectinload(User.orders))
372
373 # selectin-load Order.items and then Item.keywords
374 select(Order).options(
375 selectinload(Order.items).selectinload(Item.keywords)
376 )
377
378 # lazily load Order.items, but when Items are loaded,
379 # selectin-load the keywords collection
380 select(Order).options(lazyload(Order.items).selectinload(Item.keywords))
381
382 :param recursion_depth: optional int; when set to a positive integer
383 in conjunction with a self-referential relationship,
384 indicates "selectin" loading will continue that many levels deep
385 automatically until no items are found.
386
387 .. note:: The :paramref:`_orm.selectinload.recursion_depth` option
388 currently supports only self-referential relationships. There
389 is not yet an option to automatically traverse recursive structures
390 with more than one relationship involved.
391
392 Additionally, the :paramref:`_orm.selectinload.recursion_depth`
393 parameter is new and experimental and should be treated as "alpha"
394 status for the 2.0 series.
395
396 .. versionadded:: 2.0 added
397 :paramref:`_orm.selectinload.recursion_depth`
398
399
400 .. seealso::
401
402 :ref:`loading_toplevel`
403
404 :ref:`selectin_eager_loading`
405
406 """
407 return self._set_relationship_strategy(
408 attr,
409 {"lazy": "selectin"},
410 opts={"recursion_depth": recursion_depth},
411 )
412
413 def lazyload(self, attr: _AttrType) -> Self:
414 """Indicate that the given attribute should be loaded using "lazy"

Calls 1