merge() fails for one to one relationships marked load_on_pending=True and delete_orphan

Issue #2374 resolved
Former user created an issue

AttributeError: 'symbol' object has no attribute '_sa_instance_state'

# orm/unitofwork.py
def track_cascade_events(descriptor, prop):
    """Establish event listeners on object attributes which handle
    cascade-on-set/append.

    """
    ...
    ...
    ...
    def set_(state, newvalue, oldvalue, initiator):
        # process "save_update" cascade rules for when an instance 
        # is attached to another instance
        if oldvalue is newvalue:
            return newvalue

        sess = session._state_session(state)
        if sess:
            prop = state.manager.mapper._props[key](key)
            if newvalue is not None:
                newvalue_state = attributes.instance_state(newvalue)
                if prop.cascade.save_update and \
                    (prop.cascade_backrefs or key == initiator.key) and \
                    not sess._contains_state(newvalue_state):
                    sess._save_or_update_state(newvalue_state)

            if oldvalue is not None and prop.cascade.delete_orphan:
                oldvalue_state = attributes.instance_state(oldvalue)  # <=== oldvalue is PASSIVE_NO_RESULT

                if oldvalue_state in sess._new and \
                    prop.mapper._is_orphan(oldvalue_state):
                    sess.expunge(oldvalue)
        return newvalue
    ...
    ...
    event.listen(descriptor, 'set', set_, raw=True, retval=True)

history from http://groups.google.com/group/sqlalchemy/browse_thread/thread/51b134cd70af6fe7

Comments (12)

  1. Former user Account Deleted

    hey, sorry for the nagging, but this is preventing us pushing code to several customers... is it a suitable patch to just change this:

    if oldvalue is not None and prop.cascade.delete_orphan:
    

    to this:

    if oldvalue is not None and oldvalue is not attributes.PASSIVE_NO_RESULT and prop.cascade.delete_orphan:
    

    Or am I missing something?

  2. Mike Bayer repo owner

    that's pretty much the change, then adding your test to the unit tests somewhere, yup

  3. Former user Account Deleted

    What about other attributes.PASSIVE<something> symbols? Is PASSIVE_NO_RESULT the only one we'd expect there?

  4. Mike Bayer repo owner

    NEVER_SET might or might not be something that can be returned, I see codepaths for that, though would be nice to come up with a test which hits it.

  5. Former user Account Deleted

    Both tests (essentially same test except one with None and one with object) fail before change is applied.

    Could you let me know whether this should make it in (I'd like to patch my "branch" with what will eventually be in sync).

    P.S. As it turns out, typing "yum install hg" was quite painless after all. ;)

  6. Log in to comment