MapperExtension.after_update and Session.merge(..., dont_load=True).

21 views
Skip to first unread message

klaus

unread,
Jan 14, 2008, 5:30:39 AM1/14/08
to sqlalchemy
Hi all,
sometime since version 0.4.2, the after_update hook of a
MapperExtension fires even if no SQL UPDATE statement is generated. Is
this a bug or a feature?

In my case, an object is marked as dirty because a backref has
changed, not because of any change in the object itself. A merge(...,
dont_load=True) is also part of the mix. If it's a bug, I'll try to
provide more details.

Best regards
Klaus

Michael Bayer

unread,
Jan 14, 2008, 10:16:08 AM1/14/08
to sqlal...@googlegroups.com

On Jan 14, 2008, at 5:30 AM, klaus wrote:

>
> Hi all,
> sometime since version 0.4.2, the after_update hook of a
> MapperExtension fires even if no SQL UPDATE statement is generated. Is
> this a bug or a feature?

this is a feature, the save_obj() method is including your object but
no UPDATE is emitted because no column-mapped values have changed.
However youll notice that before_update() *is* being called, which has
to since we dont know yet if we're doing an UPDATE at that point (and
before_update() can even change that outcome), so its consistent that
after_update() should be called for every before_update() method.

>
>
> In my case, an object is marked as dirty because a backref has
> changed, not because of any change in the object itself. A merge(...,
> dont_load=True) is also part of the mix. If it's a bug, I'll try to
> provide more details.

if youd like to do the same check that save_obj() is doing on an
object for "changed", just do this:

session.is_modified(instance, include_collections=False)


klaus

unread,
Jan 14, 2008, 12:52:11 PM1/14/08
to sqlalchemy
Nice, thanks!
Reply all
Reply to author
Forward
0 new messages