Re: [sqlalchemy] recommended declarative method design pattern for sessions

2010-03-27 Thread Daniel Robbins
On Mar 23, 2010, at 9:29 AM, King Simon-NFHD78 wrote:
 object_session does indeed return the session that the instance is
 already bound to (so you shouldn't close it). I didn't know what
 object_session would return if the original session had been closed, so
 I tried it:

Thanks for looking into this for me.

As an update, I have been trying to use object_session but have been struggling 
with bugs related to the session returned from object_session() turning to 
None, presumably because it is somehow getting garbage collected.

Because of this, I am going to try to use scoped_session() as this seems to be 
the preferred method for keeping things simple and reliable with session 
sharing between disparate pieces of code. This way, all my code can create a 
new session but will end up getting the same session, thus solving the 
complexity of grabbing the current object's session (I hope.)

-Daniel

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



RE: [sqlalchemy] recommended declarative method design pattern for sessions

2010-03-22 Thread King Simon-NFHD78
Daniel Robbins wrote:
 Hi All,
 
 One of the things that doesn't seem to be covered in the 
 docs, and that I'm currently trying to figure out, is the 
 recommended design pattern to use for managing sessions from 
 declarative methods calls.
 
 Consider a declarative class User, where I want to 
 implement a FindFriends() method:
 
 class User(Base):
   # declarative fields defined here
   
   def FindFriends(self):
   session = Session()
   # it's handy to use the self reference in 
 query methods:
   friends = 
 session.query(Friends).filter_by(friend=self).all()
   session.close()
   return friends
 
 Certainly, these types of methods would seem to be useful, 
 but here's a dilemma - the above code doesn't work. Because 
 the method uses a new session, which is guaranteed to not be 
 the same session that was used to retrieve the original User 
 object, the following code will fail:
 
 session = session()
 me = session.query(User).filter_by(name=Daniel).first()
 me.FindFriends()
 

See the 'How can I get the Session for a certain object' question at
http://www.sqlalchemy.org/docs/session.html#frequently-asked-questions

Basically, in your FindFriends method, replace:

   session = Session()

with:

   session = Session.object_session(self)

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] recommended declarative method design pattern for sessions

2010-03-22 Thread Daniel Robbins
On Mar 22, 2010, at 5:10 AM, King Simon-NFHD78 wrote:
 
 See the 'How can I get the Session for a certain object' question at
 http://www.sqlalchemy.org/docs/session.html#frequently-asked-questions
 
 Basically, in your FindFriends method, replace:
 
   session = Session()
 
 with:
 
   session = Session.object_session(self)

The reference documentation seems to indicate that Session.object_session() 
will return the existing session if one exists, rather than providing a new 
session that must be separately closed.

Is this correct? If so, then FindFriends() should not close the session 
acquired via Session.object_session(obj), correct?

Is it possible for object_session() to return None if the object's session was 
previously close()d?

Thanks,

Daniel

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.