[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread Michael Bayer


On Apr 29, 2008, at 5:55 PM, David Bonner wrote:


 Hi, I'm trying to write a mapper extension that notifies a daemon
 about changes made to the DB that it needs to care about.  But it
 looks like after_update() is actually getting called before the UPDATE
 is sent to the db.

 Not knowing a better way to debug it, I just threw a pdb.set_trace()
 into my after_update method to pause the process making the change,
 then queried the db directly using sql.  It looks like the change
 hasn't made it to the DB yet, even though pdb.set_trace() is being
 triggered.


after_update() is called after all UPDATE statements have been issued  
for that particular mapper.  This includes *only* the table that is  
mapped by that mapper, not any other mappers.

Is it possible that you are seeing an UPDATE being issued for an item  
that is related to your parent via a many-to-one ?   Or that you have  
multiple ME's with after_update() at play and perhaps you're seeing a  
different one fire off.



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread Rick Morrison
 then queried the db *directly using sql*.  It looks like the change
 hasn't made it to the DB yet

Also possible is that you're using a an MVCC DB such as Postgres or Oracle,
and you're looking at an old, pre-update version of the data, as your direct
SQL would be in a separate transaction

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread David Bonner

On Wed, Apr 30, 2008 at 10:56 AM, Rick Morrison [EMAIL PROTECTED] wrote:
  then queried the db directly using sql.  It looks like the change
   hasn't made it to the DB yet

 Also possible is that you're using a an MVCC DB such as Postgres or Oracle,
 and you're looking at an old, pre-update version of the data, as your direct
 SQL would be in a separate transaction

We are using Postgres (8.1.6), but in the case of the sqlalchemy code
I've got transactional set to off for the session.  I'm not too
familiar with the specifics of MVCC (just had to google it,
actually)...is it possible the new data isn't visible to the other
process if the updating process still has a cursor live?

-- 
david bonner
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread David Bonner

On Wed, Apr 30, 2008 at 9:29 AM, Michael Bayer [EMAIL PROTECTED] wrote:
  after_update() is called after all UPDATE statements have been issued
  for that particular mapper.  This includes *only* the table that is
  mapped by that mapper, not any other mappers.

  Is it possible that you are seeing an UPDATE being issued for an item
  that is related to your parent via a many-to-one ?   Or that you have
  multiple ME's with after_update() at play and perhaps you're seeing a
  different one fire off.

It was, but I've managed to reproduce the problem with a fairly
minimal test case:

import pdb
import datetime
import pprint

from pkg_resources import require
require('SQLAlchemy==0.4.3')
from sqlalchemy import *
from sqlalchemy.orm import *

class MyExtension (MapperExtension) :
   def after_update (self, mapper, connection, instance) :
  #pdb.set_trace()
  pprint.pprint(instance)
  return EXT_CONTINUE

class Schedule (object) : pass

dburl = 'postgres://names have been changed to protect the innocent'
engine = create_engine(dburl, strategy='threadlocal')
meta = MetaData(engine)
Session = scoped_session(sessionmaker(bind=engine,
  autoflush=False,
  transactional=False))
mapper = Session.mapper
meta.reflect()
mapper(Schedule, meta.tables['schedules'], extension=MyExtension())

s = Schedule.query.first()
s.notes = str(datetime.datetime.now())
Session.flush([s])



When this code drops me into pdb, the data in instance.notes looks
like the new value, but querying the db in a separate process gets me
the old value.

-- 
david bonner
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread Rick Morrison
I suppose that depends on the behavior of the DB-API interface, in this case
I guess that's psycopg.

Anyway, I'm certainly not sure if an MVCC snapshot that's causing your
problem, but it does seem like at least a possibility. The certain way is to
check the update status inside the same transaction that did the update --
in general you can't count on transaction B to see transaction A changes
unless B starts after A commits. Whether an open pyscopg cursor implies an
open transaction -- beats me.

Rick

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread David Bonner

oops, uncommenting the pdb.set_trace(), obviously.  sorry.

  class MyExtension (MapperExtension) :
def after_update (self, mapper, connection, instance) :
   #pdb.set_trace()
   pprint.pprint(instance)
   return EXT_CONTINUE

-- 
david bonner
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread Michael Bayer


On Apr 30, 2008, at 11:22 AM, David Bonner wrote:


 When this code drops me into pdb, the data in instance.notes looks
 like the new value, but querying the db in a separate process gets me
 the old value.

flush() always uses a transaction, so when your pdb process hits, the  
transaction has not been committed yet and results are not visible  
outside of the transaction.  the transactional keyword on Session  
does not mean don't use transactions at all, it means don't  
automatically enter a transaction outside of a flush.  Its been  
renamed to autocommit in 0.5.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread David Bonner

On Wed, Apr 30, 2008 at 11:30 AM, Michael Bayer
[EMAIL PROTECTED] wrote:
  flush() always uses a transaction, so when your pdb process hits, the
  transaction has not been committed yet and results are not visible
  outside of the transaction.  the transactional keyword on Session
  does not mean don't use transactions at all, it means don't
  automatically enter a transaction outside of a flush.  Its been
  renamed to autocommit in 0.5.

Ah, yeah, that would do it.  Any suggestions for other hooks that
might do what I'm looking for, or should I just handle this myself
before and after the flush?

-- 
david bonner
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread Michael Bayer


On Apr 30, 2008, at 12:15 PM, David Bonner wrote:


 On Wed, Apr 30, 2008 at 11:30 AM, Michael Bayer
 [EMAIL PROTECTED] wrote:
 flush() always uses a transaction, so when your pdb process hits, the
 transaction has not been committed yet and results are not visible
 outside of the transaction.  the transactional keyword on Session
 does not mean don't use transactions at all, it means don't
 automatically enter a transaction outside of a flush.  Its been
 renamed to autocommit in 0.5.

 Ah, yeah, that would do it.  Any suggestions for other hooks that
 might do what I'm looking for, or should I just handle this myself
 before and after the flush?

if you want pre/post flush activities theres a SessionExtension which  
hooks into Session for that.  You can set it up with the  
sessionmaker() function so that its always plugged in.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: is MapperExtension.after_update() called before the UPDATE is issued to the DB?

2008-04-30 Thread David Bonner

On Wed, Apr 30, 2008 at 2:50 PM, Michael Bayer [EMAIL PROTECTED] wrote:
  if you want pre/post flush activities theres a SessionExtension which
  hooks into Session for that.  You can set it up with the
  sessionmaker() function so that its always plugged in.

thanks, i'll look into that.

and thanks again for the amazingly-quick and helpful replies.

-- 
david bonner
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---