[Sqlalchemy-tickets] Issue #3366: annotate parentmapper in primaryjoin / secondaryjoin (zzzeek/sqla
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2015-04-14 15:48:14
|
New issue 3366: annotate parentmapper in primaryjoin / secondaryjoin https://bitbucket.org/zzzeek/sqlalchemy/issue/3366/annotate-parentmapper-in-primaryjoin Mike Bayer: this is related to #3365 but is a more fundamental change: ``` #!diff diff --git a/lib/sqlalchemy/orm/evaluator.py b/lib/sqlalchemy/orm/evaluator.py index 1e828ff..fb59186 100644 --- a/lib/sqlalchemy/orm/evaluator.py +++ b/lib/sqlalchemy/orm/evaluator.py @@ -59,7 +59,9 @@ class EvaluatorCompiler(object): ) key = parentmapper._columntoproperty[clause].key else: - key = clause.key + raise UnevaluatableError( + "Cannot evaluate column: %s" % clause + ) get_corresponding_attr = operator.attrgetter(key) return lambda obj: get_corresponding_attr(obj) diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index b649c9e..291af09 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -1942,6 +1942,7 @@ class JoinCondition(object): self._annotate_fks() self._annotate_remote() self._annotate_local() + self._annotate_parentmapper() self._setup_pairs() self._check_foreign_cols(self.primaryjoin, True) if self.secondaryjoin is not None: @@ -2405,6 +2406,19 @@ class JoinCondition(object): self.primaryjoin, {}, locals_ ) + def _annotate_parentmapper(self): + if self.prop is None: + return + + def parentmappers_(elem): + if "remote" in elem._annotations: + return elem._annotate({"parentmapper": self.prop.mapper}) + elif "local" in elem._annotations: + return elem._annotate({"parentmapper": self.prop.parent}) + self.primaryjoin = visitors.replacement_traverse( + self.primaryjoin, {}, parentmappers_ + ) + def _check_remote_side(self): if not self.local_remote_pairs: raise sa_exc.ArgumentError( @@ -2811,9 +2825,6 @@ class JoinCondition(object): bind_to_col = dict((binds[col].key, col) for col in binds) - # this is probably not necessary - lazywhere = _deep_deannotate(lazywhere) - return lazywhere, bind_to_col, equated_columns ``` We'd just start tracking "parentmapper" throughout all PJ/SJ conditions. |