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.



[sqlalchemy] recommended declarative method design pattern for sessions

2010-03-21 Thread Daniel Robbins
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()

It would seem to be handy if SQLAlchemy placed a reference in each declarative 
object of the session from which it originated when query was called, so then 
my code could do something like this:

class User(Base):
# declarative fields defined here

def FindFriends(self):
# note the self.session.query - the idea is that sqlalchemy's 
query() would initialize this for us
return self.session.query(Friends).filter_by(friend=self).all()

Then this would allow the following code to work:

session = session()
me = session.query(User).filter_by(name=Daniel).first()
me.FindFriends()

This would work because me.FindFriends() would now have easy access to the same 
session that was used to retrieve me -- so the objects would be compatible 
and could be easily combined in queries. This would allow many methods to be 
added to the User class that could all do various kinds of db queries without 
having to pass a session variable around manually.

My question is - what is the recommended design pattern to do what I am trying 
to do above? Passing the current session as an argument to FindFriends() seems 
cumbersome - is that the recommended approach or is there a more elegant way to 
handle it? Is my handy suggestion above something that would actually be 
useful or is there a better way to do what I am wanting to do?

(I'm trying to be a good SQLAlchemy coder and not use a global 
session=Session() for everything, as explained here: 
http://www.sqlalchemy.org/docs/session.html#frequently-asked-questions . But to 
do this, I need to find a good design pattern to use in place of a global :)

Thanks and Regards,

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.