I am in need of doing something similar (a deep copy of related objects). 
Has anyone embarked on this adventure yet and mind sharing any tips, code, 
etc? My use case is to copy a retail Order, and all OrderLineItems, 
OrderNotes, OrderStatus, etc. Thank you in advance for any help.

On Wednesday, September 10, 2008 at 11:56:37 AM UTC-5, picoplex wrote:
>
> Michael,
>
> I get the idea thanks.
>
> Before I embarked on this - I wanted to check that there was not a simple 
> way that I had overlooked.
>
> Thanks for your time.
>
> Regards,
>
> Tim
>
> 2008/9/10 Michael Bayer <mik...@zzzcomputing.com <javascript:>>
>
>>
>>
>> On Sep 10, 2008, at 4:39 AM, Tim Jones wrote:
>>
>> > Michael,
>> >
>> > Thanks for your reply.
>> >
>> > The use case is configuration management.
>> >
>> > We have complex (SQLAlchemy) objects with many relationships which
>> > we wish to up-version, i.e. we want an exact persistent copy
>> > (attribute values and relationships) of the original object, but
>> > with a different identity, which we will then modify.
>> >
>> > The original object and all its relationships should remain
>> > unchanged (other than a relationship to its successor version).
>> >
>> > We would want the copied object and all its relationships saved to
>> > the database before making changes to it.
>>
>> You'd build a copy function of your own which iterates through
>> attributes and creates new instances.   How you go about locating the
>> attributes to be copied depends on if you want only SQLAlchemy-
>> instrumented attributes, or other attributes as well which are not
>> known to SQLAlchemy.
>>
>> To get a list of all SQLAlchemy attributes for a class, use this
>> expression:
>>
>> [p.key for p in class_mapper(class).iterate_properties]
>>
>> you might want to exclude primary key mappings:
>>
>> pk_keys = set([c.key for c in class_mapper(class).primary_key])
>>
>> [p.key for p in class_mapper(class).iterate_properties if p.key not in
>> pk_keys]
>>
>> the value for each attribute can be retrieved from an instance using
>> getattr(instance, key), and you can construct new instances without
>> calling __init__ by saying class.__new__(class).
>>
>> maybe someone here can flesh this out for me, im a little busy today....
>>
>>
>>
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to