From 11947c3f1fce71a8b383eb1fc9af28a0ee0fdb80 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 1 Jun 2018 14:50:25 -0500 Subject: Strong reference parent object in association proxy Considering the reversal of #597 as well as 84420a1d0fe09d7a45e878e853aa9f5258561f8b as I am unable to reproduce the original issues from that release. The long-standing behavior of the association proxy collection maintaining only a weak reference to the parent object is reverted; the proxy will now maintain a strong reference to the parent for as long as the proxy collection itself is also in memory, eliminating the "stale association proxy" error. This change is being made on an experimental basis to see if any use cases arise where it causes side effects. Change-Id: I051334be90a343dd0e8a1f35e072075eb14b14a7 Fixes: #4268 --- lib/sqlalchemy/ext/associationproxy.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/ext/associationproxy.py b/lib/sqlalchemy/ext/associationproxy.py index acf13df46..1c28b10a1 100644 --- a/lib/sqlalchemy/ext/associationproxy.py +++ b/lib/sqlalchemy/ext/associationproxy.py @@ -14,7 +14,6 @@ See the example ``examples/association/proxied_association.py``. """ import operator -import weakref from .. import exc, orm, util from ..orm import collections, interfaces from ..sql import or_ @@ -637,22 +636,17 @@ class AssociationProxyInstance(object): class _lazy_collection(object): def __init__(self, obj, target): - self.ref = weakref.ref(obj) + self.parent = obj self.target = target def __call__(self): - obj = self.ref() - if obj is None: - raise exc.InvalidRequestError( - "stale association proxy, parent object has gone out of " - "scope") - return getattr(obj, self.target) + return getattr(self.parent, self.target) def __getstate__(self): - return {'obj': self.ref(), 'target': self.target} + return {'obj': self.parent, 'target': self.target} def __setstate__(self, state): - self.ref = weakref.ref(state['obj']) + self.parent = state['obj'] self.target = state['target'] -- cgit v1.2.1