r"""Indicate that the given attribute should be eagerly loaded from columns stated manually in the query. This function is part of the :class:`_orm.Load` interface and supports both method-chained and standalone operation. The option is used in conjunction with an e
(
self,
attr: _AttrType,
alias: Optional[_FromClauseArgument] = None,
_is_chain: bool = False,
_propagate_to_loaders: bool = False,
)
| 91 | propagate_to_loaders: bool |
| 92 | |
| 93 | def contains_eager( |
| 94 | self, |
| 95 | attr: _AttrType, |
| 96 | alias: Optional[_FromClauseArgument] = None, |
| 97 | _is_chain: bool = False, |
| 98 | _propagate_to_loaders: bool = False, |
| 99 | ) -> Self: |
| 100 | r"""Indicate that the given attribute should be eagerly loaded from |
| 101 | columns stated manually in the query. |
| 102 | |
| 103 | This function is part of the :class:`_orm.Load` interface and supports |
| 104 | both method-chained and standalone operation. |
| 105 | |
| 106 | The option is used in conjunction with an explicit join that loads |
| 107 | the desired rows, i.e.:: |
| 108 | |
| 109 | sess.query(Order).join(Order.user).options(contains_eager(Order.user)) |
| 110 | |
| 111 | The above query would join from the ``Order`` entity to its related |
| 112 | ``User`` entity, and the returned ``Order`` objects would have the |
| 113 | ``Order.user`` attribute pre-populated. |
| 114 | |
| 115 | It may also be used for customizing the entries in an eagerly loaded |
| 116 | collection; queries will normally want to use the |
| 117 | :ref:`orm_queryguide_populate_existing` execution option assuming the |
| 118 | primary collection of parent objects may already have been loaded:: |
| 119 | |
| 120 | sess.query(User).join(User.addresses).filter( |
| 121 | Address.email_address.like("%@aol.com") |
| 122 | ).options(contains_eager(User.addresses)).populate_existing() |
| 123 | |
| 124 | See the section :ref:`contains_eager` for complete usage details. |
| 125 | |
| 126 | .. seealso:: |
| 127 | |
| 128 | :ref:`loading_toplevel` |
| 129 | |
| 130 | :ref:`contains_eager` |
| 131 | |
| 132 | """ |
| 133 | if alias is not None: |
| 134 | if not isinstance(alias, str): |
| 135 | coerced_alias = coercions.expect(roles.FromClauseRole, alias) |
| 136 | else: |
| 137 | util.warn_deprecated( |
| 138 | "Passing a string name for the 'alias' argument to " |
| 139 | "'contains_eager()` is deprecated, and will not work in a " |
| 140 | "future release. Please use a sqlalchemy.alias() or " |
| 141 | "sqlalchemy.orm.aliased() construct.", |
| 142 | version="1.4", |
| 143 | ) |
| 144 | coerced_alias = alias |
| 145 | |
| 146 | elif getattr(attr, "_of_type", None): |
| 147 | assert isinstance(attr, QueryableAttribute) |
| 148 | ot: Optional[_InternalEntityType[Any]] = inspect(attr._of_type) |
| 149 | assert ot is not None |
| 150 | coerced_alias = ot.selectable |