MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / contains_eager

Method contains_eager

lib/sqlalchemy/orm/strategy_options.py:94–162  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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

Calls 2

inspectFunction · 0.90