On Dec 30, 2011, at 6:34 AM, Arturo Sevilla wrote: > > > import cPickle > dumped = cPickle.dumps(user) > assert cPickle.loads(dumped).id == 1 # ERROR > > I get "sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x1405dd0> > is not bound to a Session; attribute refresh operation cannot proceed" > > As the error informs me, I tried by doing s.add(user) and s.merge(user) > before the dumps() call without success. > > If it is of any use my original error occurs in here: > > File > "/lib/python2.6/site-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/ext/mutable.py", > line 413, in unpickle > val._parents[state.obj()] = key > File "/usr/lib/python2.6/weakref.py", line 249, in __setitem__ > self.data[ref(key, self._remove)] = value > File "/model/base.py", line 230, in __eq__ # This are my > files (I have a base class for all my entities with a __eq__ method) > return self._get_id() == other._get_id() > File "/model/base.py", line 274, in _get_id # Still in > the same class > return self.id > File > "/lib/python2.6/site-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/orm/attributes.py", > line 168, in __get__ > return self.impl.get(instance_state(instance),dict_) > AttributeError: 'User' object has no attribute '_sa_instance_state'
Ah, OK so what's happening is that _sa_instance_state hasn't yet been assigned to the User object during the unpickle process, then your __eq__() is trying to get at self.id which triggers the attribute system and requires a fully composed User object. Here you'd need to either have a different way of __eq__() functioning, which might mean maybe you look inside of obj.__dict__ for "id" first to work around this, or you wouldn't use a mutable attribute. The mutable system requires that it be able to hash the parent object in a weak key dictionary. There might be other workarounds possible, perhaps we'd modify InstanceState.__setstate__() to help here, but I'd have to think about it. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.