I ran into a situation the other day where I would create a new object but 
copy over some properties from an earlier version of the object. To do 
this, I first created the new object, then I queried the database for the 
latest copy of the object, copied properties to the new object, then added 
the new object to the session and committed. I was very surprised to 
discover that the new object (which was not added to the session) was 
returned from the database query as the "latest" object.

bar = session.query(Bar).first()

new_foo = Foo()
new_foo.bar = bar # apparently causes new_obj to be added to the session

latest_foo = 
session.query(Foo).filter_by(...).order_by(Foo.creation_date.desc()).first()
assert new_foo is latest_foo # -> True

This behavior is bizarre to me, as I'd expect to only receive objects from 
a query that I had explicitly added to the session via session.add(new_foo) 
or another slightly more implicit way like adding new_foo to a managed 
object (bar.foos.append(new_foo)). Anyway, I went back and apparently it's 
the same behavior in 0.6/0.7/0.8, so it may not be a bug. Is there a 
rationale at least? Or is it a corner case that could be fixed? The gist 
below is a full example that can be run on the above versions of SQLAlchemy.

https://gist.github.com/mmerickel/5286502

Thanks,
Michael

-- 
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