look down the group recently, there was a similar question on .copy(). this is more of a semantical question, because diff.things need be treated differently. - own references 1:1 - either None or copy of the referenced object - references m:1 - the reference itself - collecions 1:many) - either new empty collection or new collection of copies of the items - asociation - new collection conating links betwen new owner and the items. there may be more sematics involved, e.g. in bitemporal case all above are implemented as associations but are not always treated as such, copy-wise. and other cases.
svil On Tuesday 16 December 2008 15:04:03 Julien Cigar wrote: > Dear SQLAlchemy users, > > I have a mapped object called "Species" with a lot of relation() in > it. The user wants to have a "duplicate" function, so for a given > "Species" a new exact copy should be created, with all the > relation(). > > I have pasted a copy of my code here : > http://pastebin.com/f3e50a4fa > > As you can see at lines 53-55 of my paste, I just iterate on those > relations and I use a setattr() on the new object. It works > perfectly, except for the "regions" relation (line 16), where > SQLAlchemy does UPDATE statements: it replaces the > "SpeciesRegion.species_id" of the source Species with the new > duplicated Species.id, so the source Species has no more "regions" > (SpeciesRegion). It seems logical to me because SpeciesRegion is an > association object, but I wondered if there is a better way to do > it than 1) iterate of SpeciesRegion objects of the source Species, > 2) create a new SpeciesRegion, 3) Adapting SpeciesRegion.region 4) > add the new SpeciesRegion to the new Species.regions ? > > Thanks, > Julien --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
