diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2017-05-23 12:08:51 -0400 |
---|---|---|
committer | Gerrit Code Review <gerrit@awstats.zzzcomputing.com> | 2017-05-23 12:08:51 -0400 |
commit | 1d0067a873a8e701b5eb8246fa6a883521c76491 (patch) | |
tree | 9825fe51449064e39fdd0a2c1ec5bb3ed0e5b51c /lib/sqlalchemy | |
parent | 7fc7492d86f6e5ca105743a184cd07190e9f9b28 (diff) | |
parent | 1c692f019b16ff4f3eb17ea8d09731837cc3be76 (diff) | |
download | sqlalchemy-1d0067a873a8e701b5eb8246fa6a883521c76491.tar.gz |
Merge "Add clause adaptation for AliasedClass to with_parent()"
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/orm/query.py | 25 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/relationships.py | 10 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/util.py | 17 |
3 files changed, 36 insertions, 16 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): diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 43f53aec5..97adf4d8b 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -1355,10 +1355,16 @@ class RelationshipProperty(StrategizedProperty): mapperlib.Mapper._configure_all() return self.prop - def _with_parent(self, instance, alias_secondary=True): + def _with_parent(self, instance, alias_secondary=True, from_entity=None): assert instance is not None + adapt_source = None + if from_entity is not None: + insp = inspect(from_entity) + if insp.is_aliased_class: + adapt_source = insp._adapter.adapt_clause return self._optimized_compare( - instance, value_is_parent=True, alias_secondary=alias_secondary) + instance, value_is_parent=True, adapt_source=adapt_source, + alias_secondary=alias_secondary) def _optimized_compare(self, state, value_is_parent=False, adapt_source=None, diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index eebe18837..9a397ccf3 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -974,7 +974,7 @@ def outerjoin(left, right, onclause=None, full=False, join_to_left=None): return _ORMJoin(left, right, onclause, True, full) -def with_parent(instance, prop): +def with_parent(instance, prop, from_entity=None): """Create filtering criterion that relates this query's primary entity to the given related instance, using established :func:`.relationship()` configuration. @@ -985,13 +985,6 @@ def with_parent(instance, prop): Python without the need to render joins to the parent table in the rendered statement. - .. versionchanged:: 0.6.4 - This method accepts parent instances in all - persistence states, including transient, persistent, and detached. - Only the requisite primary key/foreign key attributes need to - be populated. Previous versions didn't work with transient - instances. - :param instance: An instance which has some :func:`.relationship`. @@ -1000,6 +993,12 @@ def with_parent(instance, prop): 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. + + .. versionadded:: 1.2 + """ if isinstance(prop, util.string_types): mapper = object_mapper(instance) @@ -1007,7 +1006,7 @@ def with_parent(instance, prop): elif isinstance(prop, attributes.QueryableAttribute): prop = prop.property - return prop._with_parent(instance) + return prop._with_parent(instance, from_entity=from_entity) def has_identity(object): |