summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/query.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-05-22 15:08:10 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-05-23 11:13:18 -0400
commit1c692f019b16ff4f3eb17ea8d09731837cc3be76 (patch)
tree9829896302c427fe2751ebc862640578a3f882dc /lib/sqlalchemy/orm/query.py
parenteed7888f85a4255390e2522dbd428cbfe7a08bab (diff)
downloadsqlalchemy-1c692f019b16ff4f3eb17ea8d09731837cc3be76.tar.gz
Add clause adaptation for AliasedClass to with_parent()
Fixed bug where :meth:`.Query.with_parent` would not work if the :class:`.Query` were against an :func:`.aliased` construct rather than a regular mapped class. Also adds a new parameter :paramref:`.util.with_parent.from_entity` to the standalone :func:`.util.with_parent` function as well as :meth:`.Query.with_parent`. Change-Id: Ic684dd63cc90b582c7580c9bba3c92fa3f286da7 Fixes: #3607
Diffstat (limited to 'lib/sqlalchemy/orm/query.py')
-rw-r--r--lib/sqlalchemy/orm/query.py25
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index f1734194a..19a7b07c1 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -968,7 +968,7 @@ class Query(object):
"""
self._invoke_all_eagers = value
- def with_parent(self, instance, property=None):
+ def with_parent(self, instance, property=None, from_entity=None):
"""Add filtering criterion that relates the given instance
to a child object or collection, using its attribute state
as well as an established :func:`.relationship()`
@@ -981,16 +981,31 @@ class Query(object):
that the given property can be None, in which case a search is
performed against this :class:`.Query` object's target mapper.
+ :param instance:
+ An instance which has some :func:`.relationship`.
+
+ :param property:
+ String property name, or class-bound attribute, which indicates
+ what relationship from the instance should be used to reconcile the
+ parent/child relationship.
+
+ :param from_entity:
+ Entity in which to consider as the left side. This defaults to the
+ "zero" entity of the :class:`.Query` itself.
+
"""
+ if from_entity:
+ entity_zero = inspect(from_entity)
+ else:
+ entity_zero = self._entity_zero()
if property is None:
- mapper_zero = self._mapper_zero()
mapper = object_mapper(instance)
for prop in mapper.iterate_properties:
if isinstance(prop, properties.RelationshipProperty) and \
- prop.mapper is mapper_zero:
+ prop.mapper is entity_zero.mapper:
property = prop
break
else:
@@ -998,11 +1013,11 @@ class Query(object):
"Could not locate a property which relates instances "
"of class '%s' to instances of class '%s'" %
(
- self._mapper_zero().class_.__name__,
+ entity_zero.mapper.class_.__name__,
instance.__class__.__name__)
)
- return self.filter(with_parent(instance, property))
+ return self.filter(with_parent(instance, property, entity_zero.entity))
@_generative()
def add_entity(self, entity, alias=None):