I advocate using an explicit copy method for this use case. Its true that you can't just copy the full __dict__ of the object along.
At its core the state which is required is that the object has an "_sa_instance_state" attribute which represents a transient object (as opposed to a persistent state, which is what you get when you load the object). If you copy the existing _sa_instance_state, SQLA will see the object as already persistent in the database. The easiest way to get that state is to just instantiate a new object normally, i.e. newobject = MyObject(). You then want to get the correct attribute state present, while at the same time triggering the "change" events which SQLAlchemy uses to determine what data needs to be inserted. For that you set an attribute, i.e. newobject.foo = 'bar' or setattr(newobject, 'foo', 'bar'). Hence doing it explcitly, like by using __deepcopy__ (see the python docs for copy.deepcopy), makes all this as straightforward as possible: class MyClass(object): def __deepcopy__(self, memo): newobj = MyClass() # copy scalar attributes for attr in ('foo', 'bar', 'bat'): setattr(newobj, attr, getattr(self, attr)) # copy references to other objects for ref in ('x', 'y', 'z'): setattr(newobj, attr, deepcopy(getattr(self, attr))) return newobj If you really want to copy lots of data between two databases though, its probably a lot more efficient to not use an ORM for that, and to use either explicit INSERT constructs or the bulk update tools supplied with the database. On Jan 17, 2009, at 8:06 AM, Darren Govoni wrote: > > Yes, i'd like to do that, but because of the code injection seems to > carry state from the old session, the new session has trouble with > even > the copy. > > On Sat, 2009-01-17 at 15:23 +0200, a...@svilendobrev.com wrote: >> read various threads about copy/deep-copy. >> in any case u'll have to do the hierarchy-copying yourself - even if >> moving one object (alone) from session to session succeeds somehow. >> >> On Saturday 17 January 2009 14:47:41 Darren Govoni wrote: >>> After some further experimenting with this, it seems I cannot take >>> a mapped object I retrieved from one session and save it in >>> another. >>> >>> Using expunge then adding the object to a second session on a >>> different database does not work. I thought maybe it would. >>> >>> This is a really useful thing to do, especially for replication at >>> the object layer. >>> >>> So is it true that this cannot currently be done with sqlalchemy? I >>> have been reading the docs, but no clues yet. >>> >>> On Fri, 2009-01-16 at 16:53 -0500, Darren Govoni wrote: >>>> Hi, >>>> I have an object (with references) I get from a session on >>>> database A and I want to copy it (deep copy) to database B. I >>>> tried expunging it from the first session and adding it to the >>>> second. What's the best practice to do this? >>>> >>>> thanks! >>>> Darren >> >>> > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---