There are three files:
setup.py
test1.py
test2.py
1. run setup.py to create the sqlite db file
2. run test1.py to create a user and pickle it
3. run test2.py to unpickle the user, check its attributes, and save it to the session
** Make sure test1 and test2 are run in seperate python processes
The assertion in test2.py fails for me. If i take it out, then the save_or_update fails with this error:
sqlalchemy.exceptions.InvalidRequestError: Detected a mapped object not present
in the current thread's Identity Map: '(<class 'setup.User'>, (1,), None)'. Use
objectstore.import_instance() to place deserialized instances or instances from
other threads
On 7/10/06, Michael Carter <[EMAIL PROTECTED]> wrote:
er thats a little weird, when you unpickle the object, have all the
relevant classes had the same Mappers set up on them ? the _state
attribute is a class-level property accessor that will only be there
if the class has been set up with a mapper (as are all the other
properties for which youre looking for default values). the actual
data for _state is in the __dict__ as "_<classname>__sa_attr_state".Yeah, I definitely have the relevant classes set up the same when I pickle and unpickle the object, with the same mapper set up on both.
this is all class-level stuff thats not related to the instance being
in or out of a Session.
I know its class-level stuff, but what it has to do with being in or out of a Session is that you can't put it into the session if it doesn't have a _state attribute.
I will attempt to write a simple test case.
-Michael
import sqlalchemy as sa __all__ = [ 'session', 'User' ]
class User(object): pass metadata = sa.BoundMetaData('sqlite:///test.db') users = sa.Table('users', metadata, sa.Column('id', sa.Integer, primary_key=True), sa.Column('name', sa.String), sa.Column('email', sa.String)) users.mapper = sa.mapper(User, users) session = sa.create_session() if __name__ == "__main__": metadata.engine.echo = True users.create()
try: import cPickle as pickle except: import pickle from setup import * u = User() u.name = 'Michael' session.save(u) session.flush() session.expunge(u) f = open('pickled_user', 'w') pickle.dump(u, f) f.close()
try: import cPickle as pickle except: import pickle from setup import * f = open('pickled_user') u = pickle.load(f) f.close() assert hasattr(u, '_state'), "%s has no attribute _state" % u session.save_or_update(u) u.email = 'hi' session.flush()
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users