From f14a58dea4b825beb4baaef44389880927543cc4 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 20 Oct 2017 14:34:16 -0400 Subject: Resolve AliasedClass when determining owning class of association proxy Fixed bug where the association proxy would inadvertently link itself to an :class:`.AliasedClass` object if it were called first with the :class:`.AliasedClass` as a parent, causing errors upon subsequent usage. Change-Id: I9161bab67766bb75d73ca54d712ad1cad6de40dc Fixes: #4116 --- lib/sqlalchemy/ext/associationproxy.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/ext/associationproxy.py b/lib/sqlalchemy/ext/associationproxy.py index 16a4f3540..c0dbb538e 100644 --- a/lib/sqlalchemy/ext/associationproxy.py +++ b/lib/sqlalchemy/ext/associationproxy.py @@ -19,6 +19,7 @@ import weakref from .. import exc, orm, util from ..orm import collections, interfaces from ..sql import not_, or_ +from .. import inspect def association_proxy(target_collection, attr, **kw): @@ -245,7 +246,17 @@ class AssociationProxy(interfaces.InspectionAttrInfo): def __get__(self, obj, class_): if self.owning_class is None: - self.owning_class = class_ and class_ or type(obj) + try: + insp = inspect(class_) + except exc.NoInspectionAvailable: + pass + else: + if hasattr(insp, 'mapper'): + self.owning_class = insp.mapper.class_ + + if self.owning_class is None: + self.owning_class = type(obj) + if obj is None: return self -- cgit v1.2.1