Kent wrote: > I agree I shouldn't care, so maybe there is another way to attack my > problem. The reason I care is because I've extended the python object > with some auxiliary information that I need. After the refresh() in > this case, I still need access to that data that is tied to the > object, but not present in the database (it is transient data). If > sqla creates a new instance, I loose that data. > > Is there a better mechanism for doing that? > >
You need to either manually keep strong references to each object that has the auxiliary information or disable the weak identity map. See http://www.sqlalchemy.org/docs/05/session.html#frequently-asked-questions or http://www.sqlalchemy.org/docs/05/session.html#frequently-asked-questions for more information. -Conor > > On Mar 4, 3:38 pm, "Michael Bayer" <mike...@zzzcomputing.com> wrote: > >> refresh doesn't remove any objects from the session so its a matter of >> what is present in the session, not marked as dirty, and strongly >> referenced on the outside. if you're using refresh you shouldn't care >> about how it gets data back into the collection. >> >> Kent wrote: >> >>> What's strange is that I can't recreate the problem on more simple >>> stage. Every time I refresh() on the parent object, the list objects >>> remain the same. In other words, *sometimes* it behaves as I hope it >>> to (by apparently refreshing the list's objects) and *sometimes* if >>> throws them out and creates new ones. The mystery to me is what >>> determines when it will create new instances vs. refreshing the >>> existing ones? >>> >>> On Mar 4, 3:24 pm, "Michael Bayer" <mike...@zzzcomputing.com> wrote: >>> >>>> Kent wrote: >>>> >>>>> If I use session.refresh(obj) to re-load an obj that has a one-to-many >>>>> relational property, the objects in the list are *replaced* instead of >>>>> *refreshed* if they already exist. >>>>> >>>>> Suppose department has a list of employees: >>>>> >>>>> suppose dept.employees = [ emp1, emp2 ] >>>>> >>>>> session.refresh(dept) >>>>> >>>>> the dept."employees" list's elements are replaced with new objects >>>>> instead of reusing those that existed and refreshing them. >>>>> >>>>> Is it possible to have those same objects re-used and simply refreshed >>>>> instead of replaced? >>>>> >>>> you can only turn off "refresh-expire" cascade, which will prohibit the >>>> operation from traveling into the child objects. the collection is >>>> still >>>> refreshed for obvious reasons, its one of the attributes on your mapped >>>> object. >>>> >>>> To achieve your specified behavior, use session.refresh() given as its >>>> second argument the set of attribute names which are safe to be reloaded >>>> completely (in this case the scalars). Then for each uselist >>>> attribute, >>>> iterate the collection of each and call the desired version of >>>> session.refresh() for those. >>>> >>>> This is an easy refresh() function to create in a generalized way by >>>> inspecting the class-level attributes of the incoming object. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.