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

Function with_parent

lib/sqlalchemy/orm/util.py:1960–2033  ·  view source on GitHub ↗

Create filtering criterion that relates this query's primary entity to the given related instance, using established :func:`_orm.relationship()` configuration. E.g.:: stmt = select(Address).where(with_parent(some_user, User.addresses)) The SQL rendered is the same as t

(
    instance: object,
    prop: attributes.QueryableAttribute[Any],
    from_entity: Optional[_EntityType[Any]] = None,
)

Source from the content-addressed store, hash-verified

1958
1959
1960def with_parent(
1961 instance: object,
1962 prop: attributes.QueryableAttribute[Any],
1963 from_entity: Optional[_EntityType[Any]] = None,
1964) -> ColumnElement[bool]:
1965 """Create filtering criterion that relates this query's primary entity
1966 to the given related instance, using established
1967 :func:`_orm.relationship()`
1968 configuration.
1969
1970 E.g.::
1971
1972 stmt = select(Address).where(with_parent(some_user, User.addresses))
1973
1974 The SQL rendered is the same as that rendered when a lazy loader
1975 would fire off from the given parent on that attribute, meaning
1976 that the appropriate state is taken from the parent object in
1977 Python without the need to render joins to the parent table
1978 in the rendered statement.
1979
1980 The given property may also make use of :meth:`_orm.PropComparator.of_type`
1981 to indicate the left side of the criteria::
1982
1983
1984 a1 = aliased(Address)
1985 a2 = aliased(Address)
1986 stmt = select(a1, a2).where(with_parent(u1, User.addresses.of_type(a2)))
1987
1988 The above use is equivalent to using the
1989 :func:`_orm.with_parent.from_entity` argument::
1990
1991 a1 = aliased(Address)
1992 a2 = aliased(Address)
1993 stmt = select(a1, a2).where(
1994 with_parent(u1, User.addresses, from_entity=a2)
1995 )
1996
1997 :param instance:
1998 An instance which has some :func:`_orm.relationship`.
1999
2000 :param property:
2001 Class-bound attribute, which indicates
2002 what relationship from the instance should be used to reconcile the
2003 parent/child relationship.
2004
2005 :param from_entity:
2006 Entity in which to consider as the left side. This defaults to the
2007 "zero" entity of the :class:`_query.Query` itself.
2008
2009 .. versionadded:: 1.2
2010
2011 """ # noqa: E501
2012 prop_t: RelationshipProperty[Any]
2013
2014 if isinstance(prop, str):
2015 raise sa_exc.ArgumentError(
2016 "with_parent() accepts class-bound mapped attributes, not strings"
2017 )

Callers 15

test_no_stringsMethod · 0.90
test_o2mMethod · 0.90
test_invalid_propertyMethod · 0.90
test_select_fromMethod · 0.90
test_noparentMethod · 0.90
test_m2mMethod · 0.90
test_with_transientMethod · 0.90

Calls 2

prop_is_relationshipFunction · 0.85
_with_parentMethod · 0.80

Tested by 15

test_no_stringsMethod · 0.72
test_o2mMethod · 0.72
test_invalid_propertyMethod · 0.72
test_select_fromMethod · 0.72
test_noparentMethod · 0.72
test_m2mMethod · 0.72
test_with_transientMethod · 0.72