yup, before_flush is made for that, and I've for some time had some vague plans 
to add some more helpers there so you could get events local to certain kinds 
of objects in certain kinds of states, meaning it would look a lot like 
before_update.   But looping through .new, .dirty, and .deleted is how to do it 
for now.



On Jan 26, 2012, at 12:12 PM, Kent wrote:

> Fair enough.  I had enough understanding of what must be going on to know 
> flush isn't straightforward, but I'm still glad I asked.  Sorry for having 
> not read the documents very well and thanks for your answer, because from it, 
> I surmise that before_flush() *is* safe for session operations, which is very 
> good to understand more clearly.
> 
> Thanks.
> 
> On 1/26/2012 12:06 PM, Michael Bayer wrote:
>> On Jan 26, 2012, at 11:28 AM, Kent Bower wrote:
>> 
>>> I think I understand why, during a flush(), if I use session.query().get() 
>>> for an item that was just added during this flush, I don't get the 
>>> persistent object I might expect because the session still has it as 
>>> pending even though, logically, it is already persistent.
>>> 
>>> I don't suppose you have any desire to support that, huh?  The use case 
>>> would be related to the future ticket 
>>> http://www.sqlalchemy.org/trac/ticket/1939 (and 
>>> http://www.sqlalchemy.org/trac/ticket/2350).
>>> 
>>> Attached is a script demonstrating the issue I've hit.  I can work around 
>>> it with some difficulty, but I wanted your input and thoughts.
>> No, there's no plans to support this case at all; you're using the Session 
>> inside of a mapper event, which is just not supported, and can never be due 
>> to the nature of the unit of work.   The most recent docstrings try to be 
>> very explicit about this:
>> 
>> http://docs.sqlalchemy.org/en/latest/orm/events.html#sqlalchemy.orm.events.MapperEvents.before_update
>> 
>> I guess I have to add session.query() and get() in there as well.
>> 
>> The way the flush works is not as straightforward as "persist object A; 
>> persist object B; persist object C" - that is, these are not atomic 
>> operations inside the flush.    It's more like, "Perform step X for objects 
>> A, B, and C; perform step Y for objects A, B and C".   This is basically 
>> batching, and is necessary since it is vastly more efficient than atomically 
>> completing each object one at a time.   Also, some decisions are needed by Y 
>> which can't always be made until X has completed for objects involved in 
>> dependencies.
>> 
>> A side effect of batching is that if we provide a hook that emits after X 
>> and before Y, you're being exposed to the objects in an unusual state.   
>> Hence, the hooks that are in the middle like that are only intended to emit 
>> SQL on the given Connection; not to do anything ORM level beyond assigning 
>> column-based values on the immediate object.    As always, before_flush() is 
>> where ORM-level manipulations are intended to be placed.
>> 
>> 
> 
> -- 
> 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 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to