On Jan 20, 2008, at 10:03 AM, VitaminJ wrote:
> > Hi! > > Is there a way to find out about the state an object is in? I am > particular interested to find out if there is the need to call > session.update() for an object or if the identidy is already contained > in the session. I cannot manage to do this, but get an Invalid Request > Errror. > > So the example to get the InvalidRequestErrro goes as follows: > > session = Session() > obj1 = session.query(Advertisment).get(1) > session.expunge(obj1) > obj2 = session.query(Advertisment).get(1) > session.update(obj1) # raises InvalidRequestError > > I would like to do something like: > > if not obj1 in session: > session.update(obj1) > > Any help would be appreciated very much. > Thanks, Jan well actually you *can* do exactly "if obj1 not in session: session.update(obj1)", but what that does is tell you if "obj1" specifically is in the session, not any object with the same identity as obj1. if you want to check for a certain identity, use the identity_map dictionary directly, using "session.identity_key(instance=obj1) in session.idenitity_map". But i think what suits your case above even better is merge(); if you wanted to turn "obj1" into "obj2", and copy all the changes present on obj1 into the session, you could say: obj1 = session.merge(obj1) the merge() call will return the current persistent object if present, else will load it from the database using obj1's identity. In both cases all of obj1's attributes are merged into the persistent instance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
