I often run into problems when working with SA's events.
MapperExtension's methods are called in the middle of a flush, and so
there are things to take care of. For example:

  * if you add new objects to the session, or do an update during an
event, you'll have to call session.flush() a second time for them to
be actually inserted or updated (and you can't call flush() within the
event since you're in the middle of a flush). There is a workaround
for this issue, you use a SessionExtension that looks for dirty
objects in its after_flush_postexec() method, and call flush() a
second time if there is any [1].

  * if an error occurs during an event (e.g. a database timeout), you
can't handle it within the event with a rollback or else you'll get an
"InvalidRequestError: The transaction is inactive due to a rollback in
a subtransaction. Issue rollback() to cancel the transaction." error
on the next usage of the transaction [2].

While the first problem just involves a quick hack (which you still
have to figure out), the second is more annoying and involves coupling
(you have to know that the particular table you are flushing triggers
events and needs special error handling).

I think it would make sense to add new events triggered *outside* of
the flush mechanism. For example the order of operations for an update
would look something like this :

outer_before_update()
[begin]
before_update()
[emit SQL update]
after_update()
[commit]
outer_after_update()

This way you would code normally in events, as you do in the rest of
your code, without taking care of special rules.

Maybe MapperExtension is not the right place to add these kind of
events (as I see it, it's more a tool to extend the API than a day to
day tool to work with events), and something like MapperEvents should
be created instead ?

[1] http://paste.pocoo.org/show/386418/
[2] http://paste.pocoo.org/show/386426/

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

Reply via email to