MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / with_parent

Method with_parent

lib/sqlalchemy/orm/query.py:1263–1330  ·  view source on GitHub ↗

Add filtering criterion that relates the given instance to a child object or collection, using its attribute state as well as an established :func:`_orm.relationship()` configuration. The method uses the :func:`.with_parent` function to generate the clause, t

(
        self,
        instance: object,
        property: Optional[  # noqa: A002
            attributes.QueryableAttribute[Any]
        ] = None,
        from_entity: Optional[_ExternalEntityType[Any]] = None,
    )

Source from the content-addressed store, hash-verified

1261 )
1262 @util.preload_module("sqlalchemy.orm.relationships")
1263 def with_parent(
1264 self,
1265 instance: object,
1266 property: Optional[ # noqa: A002
1267 attributes.QueryableAttribute[Any]
1268 ] = None,
1269 from_entity: Optional[_ExternalEntityType[Any]] = None,
1270 ) -> Self:
1271 """Add filtering criterion that relates the given instance
1272 to a child object or collection, using its attribute state
1273 as well as an established :func:`_orm.relationship()`
1274 configuration.
1275
1276 The method uses the :func:`.with_parent` function to generate
1277 the clause, the result of which is passed to
1278 :meth:`_query.Query.filter`.
1279
1280 Parameters are the same as :func:`.with_parent`, with the exception
1281 that the given property can be None, in which case a search is
1282 performed against this :class:`_query.Query` object's target mapper.
1283
1284 :param instance:
1285 An instance which has some :func:`_orm.relationship`.
1286
1287 :param property:
1288 Class bound attribute which indicates
1289 what relationship from the instance should be used to reconcile the
1290 parent/child relationship.
1291
1292 :param from_entity:
1293 Entity in which to consider as the left side. This defaults to the
1294 "zero" entity of the :class:`_query.Query` itself.
1295
1296 """
1297 relationships = util.preloaded.orm_relationships
1298
1299 if from_entity:
1300 entity_zero = inspect(from_entity)
1301 else:
1302 entity_zero = _legacy_filter_by_entity_zero(self)
1303 if property is None:
1304 # TODO: deprecate, property has to be supplied
1305 mapper = object_mapper(instance)
1306
1307 for prop in mapper.iterate_properties:
1308 if (
1309 isinstance(prop, relationships.RelationshipProperty)
1310 and prop.mapper is entity_zero.mapper # type: ignore
1311 ):
1312 property = prop # type: ignore # noqa: A001
1313 break
1314 else:
1315 raise sa_exc.InvalidRequestError(
1316 "Could not locate a property which relates instances "
1317 "of class '%s' to instances of class '%s'"
1318 % (
1319 entity_zero.mapper.class_.__name__, # type: ignore
1320 instance.__class__.__name__,

Calls 5

filterMethod · 0.95
inspectFunction · 0.90
object_mapperFunction · 0.85
with_parentFunction · 0.85