On Dec 7, 2010, at 7:46 AM, Ian Thompson wrote: > On Dec 6, 5:53 pm, Michael Bayer <[email protected]> wrote: >> On Dec 6, 2010, at 11:52 AM, Ian Thompson wrote: >> >> >> >> >> >>> On Dec 6, 4:49 pm, Ian Thompson <[email protected]> wrote: >>>> I've generated a test data set and then want to have all of the >>>> created objects and relationships available for reference easily. >>>> To avoid possible pollution of this fixture I want to detach the >>>> objects from the sessions to avoid them getting any updates applied. >> >>>> To do this I am attempting to load all data via joinedload and then >>>> calling session.expunge_all: >> >>>> print assets['test_asset_1'].attributes # OK >> >>>> session.query(Asset).options(joinedload(Asset.asset_attributes)).populate_e >>>> xisting().all() >>>> session.expunge_all() >>>> print assets['test_asset_1'].attributes # ERROR >> >>>> None of the relationships on the objects survive the expunge. >> >>>> sqlalchemy.orm.exc.DetachedInstanceError: >>>> Parent instance <Asset at ...> is not bound to a Session; >>>> lazy load operation of attribute 'asset_attributes' cannot proceed >>> (cont...) >>> Is there a correct way to detach all current data from the Session >>> fully loading any relationships? (Small data set so memory is not a >>> concern.) >> >> This use case seems like it would be more easily solved using a new Session >> just for that load. To work with the objects detached, all "deferred" >> attributes and lazy-loading "relationship" attributes which you will need to >> access would need to either be eagerly loaded (there's now three ways to >> eagerly load relationship() attributes), or otherwise accessed via >> obj.attribute before the objects are expunged. > > Hi Michael, thanks for your reply. > > I had thought by using joinedload (or eagerload) and populate_existing > I would be doing an eager load of the specified relation. > > Also, in my example I do access the attributes relationship (with a > print), then after the expunge the same fails. Is there a way I can > ensure the data is preserved after expunging?
expunge() doesn't affect the data associated with the instances. Its possible the populate_existing() is interfering with the joined load, I'd check the status of asset_attributes right after the query. > > Thanks > Ian > > -- > 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. > -- 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.
