[sqlalchemy] objects not necessarily pulled from session's identity map when they should (?)

2014-11-04 Thread Jonathan Vanasco
I've been going batty on this all morning.

I have a permissions check routine that repeatedly queries for a certain 
Foo2Bar table

class Foo2Bar(Base):
__tablename__ = 'foo_2_bar'
id_foo = Column(Integer, ForeignKey(foo.id), primary_key=True)
id_bar = Column(Integer, ForeignKey(bar.id), primary_key=True)

print Get Foo2Bar() 4x
print id(s.query(Foo2Bar).get((1,2)))
print id(s.query(Foo2Bar).get((1,2)))
print id(s.query(Foo2Bar).get((1,2)))
print id(s.query(Foo2Bar).get((1,2)))

When I do this on a test harness, it works exactly like it should.  It only 
hits the database once.

When I do this in my app, it hits the database (postgres) unreliably.

I set up extensive logging and used a bunch of breakpoints with pdb.  
The session is the same at every point, there is an object already in the 
identity map for the composite key -- but it appears to get overridden each 
time.

If i put a `get()` in a loop 40x, it only gets the first one.  but every 
prior request is pulled from the database.

does anyone have a clue what could cause this behavior?


-- 
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/d/optout.


Re: [sqlalchemy] objects not necessarily pulled from session's identity map when they should (?)

2014-11-04 Thread Claudio Freire
On Tue, Nov 4, 2014 at 3:15 PM, Jonathan Vanasco jvana...@gmail.com wrote:

 I have a permissions check routine that repeatedly queries for a certain
 Foo2Bar table

 class Foo2Bar(Base):
 __tablename__ = 'foo_2_bar'
 id_foo = Column(Integer, ForeignKey(foo.id), primary_key=True)
 id_bar = Column(Integer, ForeignKey(bar.id), primary_key=True)

 print Get Foo2Bar() 4x
 print id(s.query(Foo2Bar).get((1,2)))
 print id(s.query(Foo2Bar).get((1,2)))
 print id(s.query(Foo2Bar).get((1,2)))
 print id(s.query(Foo2Bar).get((1,2)))

 When I do this on a test harness, it works exactly like it should.  It only
 hits the database once.

 When I do this in my app, it hits the database (postgres) unreliably.

...

 does anyone have a clue what could cause this behavior?

The code as shown, would likely exhibit that behavior with a weak
identity map. It would indeed be hard to predict when it happened
since weakrefs are cleared on garbage collection cycles, and those
happen at hard to predict times.

You could rule that out, by storing the result of get() in some
variable somewhere for the duration of the test/routine. That should
keep it in the identity map long enough to serve your purposes.

-- 
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/d/optout.


Re: [sqlalchemy] objects not necessarily pulled from session's identity map when they should (?)

2014-11-04 Thread Jonathan Vanasco
Thanks!

I didn't realize that objects are cleaned up with scope like normal python 
objects.  I thought they were in the session for the lifetime of the 
session.  This was driving me crazy.

This is just a web request, so I'm now appending the result into 
`request.persistanceArray`.  instantly fixed my problem.


-- 
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/d/optout.