So my code above is just completely wrong.  

This code actually does what one expects:

    def recursive_expunge(obj, dbSession):
        def _recursive_expunge(_obj):
            _instance_state = sqlalchemy.inspection.inspect(_obj)
            _mapper = _instance_state.mapper
            try:
                dbSession.expunge(_obj)
                # print "expunge | %s" % _obj
            except sqlalchemy.orm.exc.UnmappedInstanceError:
                # print "sqlalchemy.orm.exc.UnmappedInstanceError | %s" % 
_obj
                pass
            except sqlalchemy.exc.InvalidRequestError:
                # print "sqlalchemy.exc.UnmappedInstanceError | %s" % _obj
                pass
            if _mapper:
                # _unloaded = [(_name, _rel) for (_name, _rel) in 
_mapper.relationships.items() if _name in _instance_state.unloaded]
                _loaded_rels = [i for i in _mapper.relationships.items() if 
i[0] not in _instance_state.unloaded]
                for (_name, _rel) in _loaded_rels:
                    _loaded_rel_data = getattr(_obj, _name)
                    if _loaded_rel_data:
                        if not _rel.uselist:
                            _recursive_expunge(_loaded_rel_data)
                        else:
                            for _i in _loaded_rel_data:
                                _recursive_expunge(_i)
        _recursive_expunge(obj)

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to