Re: [sqlalchemy] filter vs get question

2014-02-20 Thread Claudio Freire
On Thu, Feb 20, 2014 at 5:22 PM, Jonathan Vanasco jonat...@findmeon.com wrote:
 this seems to work, but I want to just make sure this is the intended
 behavior.

a = dbSession.query( Something ).filter( Something.primary_key == 1
 ).first()
b = dbSession.query( Something ).get( 1 )
c = dbSession.query( Something ).get( 1 )
d = dbSession.query( Something ).get( 1 )
e = dbSession.query( Something ).get( 1 )

 in the above example , we will only hit the database once , because the
 'filter' populates the local session map with the primary key.  right ?


Only as long as you keep a reference to the object returned by the
first query, since the identity map is a weak map.

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] filter vs get question

2014-02-20 Thread Jonathan Vanasco
that's fine.  this is for a webapp where we have in a single request :


   begin;
   user = .filter().first()
   DO LOTS OF STUFF, all over the place
   DO EVEN MORE STUFF   , in more places
   user = .get(user_id)
   commit;

if this behavior is intended, then we can just rely on it for now. 
 otherwise we need to refactor a lot of code to explicitly pass around the 
User object.  until today, we only needed to operate on user_ids , now we 
need to check a value on the user object in 5 places (only on a particular 
url).  i don't want to hit the db 5x.

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] filter vs get question

2014-02-20 Thread Michael Bayer

On Feb 20, 2014, at 3:22 PM, Jonathan Vanasco jonat...@findmeon.com wrote:

 this seems to work, but I want to just make sure this is the intended 
 behavior.
 
a = dbSession.query( Something ).filter( Something.primary_key == 1 
 ).first()
b = dbSession.query( Something ).get( 1 )
c = dbSession.query( Something ).get( 1 )
d = dbSession.query( Something ).get( 1 )
e = dbSession.query( Something ).get( 1 )
 
 in the above example , we will only hit the database once , because the 
 'filter' populates the local session map with the primary key.  right ?

yes.   As long as you maintain a reference to the object outside of the session.

I use this pattern when I am dealing with lots of data that has a bunch of many 
to ones.  Suppose Player objects have a many-to-one to a Sport.  We have large 
N number of players and just a handful of Sports.   So i do this:

sports = set(sess.query(Sport))  # one SELECT.  hold onto “sports” for the 
duration

for player in sess.query(Player):  # one SELECT
   # …
   player.sport   # uses get(), no SELECT

this is not too different from using “subquery eager loading” except the 
queries are straight SELECT with no joins or subqueries.





signature.asc
Description: Message signed with OpenPGP using GPGMail