On Oct 7, 2010, at 5:37 AM, Alok G. Singh wrote: > Is there an elegant way of replacing all the attributes of an instance, > except the primary key ? > > Currently, I create a new object with all the date, fetch the old object > and do for k,v in new: old.__setattr__(k, v), taking care to skip the > primary key attribute. There's a custom __iter__ method for the class > to make sure this works. > > Just setting the primary key attribute (say 'id') as new.id = old.id > doesn't do the right thing and fails with an Duplicate key error. > > To set the context, what I am attempting here is to add an "import" > function. The objects are serialised to YAML and I want to replace > rather than add in case the object already exists in the DB.
updating the DB from a serialized structure is typically accomplished via Session.merge(). Prepare a complete structure of objects from the YAML format, ensure that their primary key attributes correspond to thoes in the DB, then merge() the whole structure. -- 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.
