On Mar 17, 2014, at 8:53 AM, Dirk Makowski <dirk.makow...@gmail.com> wrote:

> On Monday, March 17, 2014 12:44:24 PM UTC+1, Michael Bayer wrote:
> the last moment you would have to validate what's to be sent as an UPDATE 
> before the structure is fixed would be the before_update mapper event.    The 
> other option would be within the before_flush() event.
> 
> Thank you for the pointer to these events. I went for the mapper event 
> before_update, and basically I could realise the intended check. But it 
> turned out, this event is only fired for 'real' instances, not for mixins. So 
> I could not write one event handler for DefaultMixin, but had to write as 
> many as I have model classes. Is that correct or is there a way to get this 
> event for mixins?
> (Attached code shows this variant.)

you can write a handler for a mixin.  It needs 0.8 at least and you set up the 
event with the flag propagate=True, meaning it propagates to subclasses.


> 
> What would have to be done to implement the check with the before_flush event?
> Iterate over the set of instances in session.dirty, filter out those who 
> actually will produce an UPDATE SQL (session.is_modified()) and perform the 
> check on those?

yes, in my own apps I'm often doing a lot of before_flush() things, so I 
usually have just one before_flush() listener that iterates through 
session.dirty and runs a series of checks on them (e.g. versioning, validation 
routines, etc).   I guess you could use is_modified() to check for a net change 
but you might want to find more of a business-level reason for the attribute to 
be present or not.

or you could just use attribute on change events to make sure that attribute 
gets the change it needs in all cases....

-- 
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.

Reply via email to