Greetings SA community!

I've got mapped objects being created and added to a session, but not
committed.  Once in a while, I'd like to do something a-la:

  oid = 42
  object = MyClass(oid)

  sessionA.add(object)

  # ... time passes, things happen ... then

  object = get_obj_from_session(oid, sessionA)
  sessionA.expunge(object)

  sessionB = gimme_a_new_session()
  sessionB.add(object)
  sessionB.commit()
  sessionB.close()

This fails to actually save the object to the db: nothing is inserted
as SA seems to think the object is an up to date reflection of some
(non-existent) row in the db.

The problem, I think, is the InstanceState associated with this object
from its initial residence in sessionA.  This state has lotsa stuff in
it, including a 'key' attribute that looks something like:

   (model.MyClass, 42)

I've read elsewhere that the "solution" is do delete the instance
state 'key', but this only hangs my program with no indication as to
why.  In any case, this seems like a solution that involves way to
much knowledge of the internals for my taste.

I've tried a number of things, everything from trying to merge() into
the new session, deleting the entire instance state from the object,
deleting + expunging from sessionA and other random things, to no
avail.

Doing a deep copy of the expunged object might be possible, to then
add that to the new session, but would involve a lot of work for
something that seems like it should be straightforward with
SQLAlchemy...

My questions are:

   - why does the instance state think it doesn't need to insert this
object, that was never committed?

   - SA seems to be acting as if the object is in a "Detached" state
after the expunge, even though it
  was never committed and does not exist in the db.  Why and can I
somehow force this to "Transient"
  before the sessionB.add() or "Pending", after?

   - why doesn't expunge remove all references to the original session/
state -- is there a way to do this?

   - maybe I'm going about this completely wrong... any other method I
should be using?




Thanks in advance and regards,
Pat Deegan

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to