On Dec 19, 2012, at 12:21 PM, Russ wrote:

> I've got some code where I want to assert that the ORM session is perfectly 
> clean. ie: I want to know if a flush() will emit SQL.
> 
> What is the best way to determine this?
> 
> Right now I'm simply checking like this:
> 
> if session.new or session.dirty or session.deleted:
>     print "flush() actions pending!"
> 
> I believe this is a complete way to check this, but I'm wondering if there is 
> a more efficient, possibly single point, way to check this.  One reason for 
> asking this is that the docs indicate for session.dirty that "this collection 
> is now created on the fly each time the property is called"... but when all I 
> want to know is "is flush() going to do anything?" it seems a waste to 
> generate that collection.

the Session uses the method session._is_clean() internally to check if going 
through the flush() steps is warranted, which is roughly equivalent to the 
check you're doing, though it is doing less work to make this decision - if you 
take a look at what it does, compared to the implementation for .new, .dirty, 
.deleted, you save on the iteration of those collections and the translation of 
all the InstanceState objects into userland objects.

There's another caveat, which is that even if _is_clean() is false, that's not 
a total guarantee that SQL will be emitted on flush.   If you changed one 
object attribute to be the same value that it already was, for example, that's 
a "dirty" event, but when the flush runs through, it will see no net change and 
not emit an UPDATE.

This is part of the reason _is_clean() at the moment still has an underscore.   
Easier to keep the underscore than to promise a completely predictable usage 
pattern :).


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