From d1326cf54960dfa16bded3789d834b088aff2c1e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 12 Mar 2008 01:52:36 +0000 Subject: [PATCH] - when attributes are expired on a pending instance, an error will not be raised when the "refresh" action is triggered and returns no result --- CHANGES | 6 +++++- lib/sqlalchemy/orm/mapper.py | 5 ++++- test/orm/expire.py | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 2c83be0cc2..2c9120be5b 100644 --- a/CHANGES +++ b/CHANGES @@ -4,7 +4,11 @@ CHANGES 0.4.5 ===== - +- orm + - when attributes are expired on a pending instance, an + error will not be raised when the "refresh" action + is triggered and returns no result + 0.4.4 ------ - sql diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index ae48f9e55f..b7ed313950 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -1565,10 +1565,13 @@ def _load_scalar_attributes(instance, attribute_names): state = instance._state if '_instance_key' in state.dict: identity_key = state.dict['_instance_key'] + shouldraise = True else: + # if instance is pending, a refresh operation may not complete (even if PK attributes are assigned) + shouldraise = False identity_key = mapper._identity_key_from_state(state) - if session.query(mapper)._get(identity_key, refresh_instance=state, only_load_props=attribute_names) is None: + if session.query(mapper)._get(identity_key, refresh_instance=state, only_load_props=attribute_names) is None and shouldraise: raise exceptions.InvalidRequestError("Could not refresh instance '%s'" % instance_str(instance)) def _state_mapper(state, entity_name=None): diff --git a/test/orm/expire.py b/test/orm/expire.py index 8d622dd0cd..58c05a3820 100644 --- a/test/orm/expire.py +++ b/test/orm/expire.py @@ -84,6 +84,14 @@ class ExpireTest(FixtureTest): except exceptions.InvalidRequestError, e: assert str(e) == "Instance is not bound to a Session, and no contextual session is established; attribute refresh operation cannot proceed" + def test_pending_doesnt_raise(self): + mapper(User, users) + sess = create_session() + u = User(id=15) + sess.save(u) + sess.expire(u, ['name']) + assert u.name is None + def test_no_instance_key(self): # this tests an artificial condition such that # an instance is pending, but has expired attributes. this -- 2.47.3