On May 16, 2013, at 9:17 PM, Bobby Impollonia <bob...@gmail.com> wrote:

> That makes sense. I am seeing one problem with this approach and it causes 
> the asserts in original gist to still fail. The problem is that the identity 
> map remembers that I constructed my objects as instances of the parent class 
> even across commit and expire boundaries. So the following assert fails:
> 
> street_affiliation = Affiliation(user=user, address_type='street', 
> address_id=1)

right well you don't do that anymore ;)  make it StreetAffiliation.  Or use a 
factory method like Affiliation.create(type="street"), or add a special __new__ 
to Affiliation to do the same thing.

> 
> I understand why this happens (the identity map uses weakrefs but there is a 
> still a reference to the old object of type Affiliation) and how to work 
> around it (delete the reference to the old affiliation before querying or 
> construct the affiliation object as a StreetAffiliation instead of as an 
> Affiliation). It is still somewhat surprising since I am used to assuming 
> that once a commit/ expire_all has happened, fetching from the database will 
> give the "right" results, but in this case the affiliation I fetch after the 
> flush incorrectly tells me that its street_address is None.

the expire removes the contents of __dict__ of all the objects, but not the 
objects themselves, which means their type is also preserved in the session.   
The objects will fall out of the session if you lose all references to them on 
the outside, but that is also assuming they are garbage collected which won't 
necessarily be immediate if there are any cycles.


-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to