Another wrinkle to this is that if I already have relationship data within 
the ORM, but then add records outside of the ORM with the expression 
language, I can't figure out how to reconcile this efficiently.

As a specific example, if I add the snippet below to my original example, 
you can see the ORM falls out of sync.

#Add some new emails to Joe outside the ORM...
initial_mail_count = len(joe.addresses)
new_mail_count = 2
new_emails = ["newjoe%...@example.com" % i for i in range(new_mail_count)]
emailValues = [dict(FK_user_id = joe.id, email = addr) for addr in 
new_emails]
sess.execute(Address.__table__.insert(), emailValues)

#Joe's new emails are not in joe.addresses...
# - because the ORM has zero awareness of the direct execute call
assert len(joe.addresses) == initial_mail_count

#I can get them by expiring all of Joe...
# - is there a way to do this without expiring Joe?
# - Can the relation be expired directly?
# - Better would be to to inform the ORM of new data (instead of expiring 
old
#   data), but I'm looking for workarounds.
sess.expire(joe)
assert len(joe.addresses) == (initial_mail_count + new_mail_count)

As per the code and comments, I can bring it back into sync by expiring the 
top level object, but this is clearly not very efficient since it results in 
a whole query for the top level object, as well as any additional eager 
loads that have been configured for that object.

Can a relation be expired directly?  I'm searching hard for how to do this 
and failing.  I'm also still very interested in whether there is some other 
efficient way to re-sync the ORM with transactions performed outside of it.

I have updated the pastebin sample code to include the above snippet as 
well:
http://pastebin.com/eCDSm0YW

Russ

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/rX5kPq4zwjEJ.
To post to this group, send email to sqlalchemy@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.

Reply via email to