Am 14.08.2010 um 17:28 schrieb Michael Hipp:
I'm obviously missing some key concept as regards the management of
sessions. This seemingly simple usage fails:
def get_new():
sess = Session()
new = Something() # new orm object
sess.add(new)
sess.commit()
sess.close()
return new
new = get_new() # request a new Something
print new
print new.id
Those last 2 print lines throw:
DetachedInstanceError: Instance <Something at 0x2873ed0> is not
bound to
a Session; attribute refresh operation cannot proceed
I seem to keep butting heads with the session needing to be a global
eternal thing (opposite what the docs recommend). I could create
another session and add 'new' to it, but that seems like a lot of
boilerplate when all I wanted to do was get a bit of info from the
returned object.
Can someone explain how this is supposed to be done?
Everything you do with enities needs to be *inside* a session scope.
It's that simple. So either make get_new() return the data you are
interested in directly, or reload the returned object in a fresh
session via refresh or reload (don't know out of my head)
Diez
--
You received this message because you are subscribed to the Google Groups
"SQLElixir" 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/sqlelixir?hl=en.