onupdate Column attribute with joined table inheritance question

116 views
Skip to first unread message

Sam Magister

unread,
Sep 2, 2008, 3:06:20 PM9/2/08
to sqlalchemy
Hi,

I'm using joined table inheritance much like the example given here:
http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_mapper_inheritance_joined

Additionally, in the employees table, I would like an update_timestamp
column of type DateTime:

Column('update_timestamp', DateTime, onupdate=datetime.datetime.now())

The rational is so I could keep track of when any employees info is
changed, be it a manager or engineer.

However, this field is not being updated when an engineer or manager
is updated. I'm pretty sure I understand why: The employee table is
not being updated.

I was just wondering if there was an easy way to get around this issue
without using MapperExtensions. Previously I was using
MapperExtensions and overriding the before_update method which works
since it looks for updates in the entire instance, not just that
table. This way works fine, but I like the cleanness of not needing
MapperExtensions.

Thanks!

Sam

Michael Bayer

unread,
Sep 2, 2008, 3:50:09 PM9/2/08
to sqlal...@googlegroups.com

On Sep 2, 2008, at 3:06 PM, Sam Magister wrote:

>
> Hi,
>
> I'm using joined table inheritance much like the example given here:
> http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_mapper_inheritance_joined
>
> Additionally, in the employees table, I would like an update_timestamp
> column of type DateTime:
>
> Column('update_timestamp', DateTime, onupdate=datetime.datetime.now())
>

Theres a common mistake here. Either have onupdate set to
datetime.datetime.now (no parenthesis), or set it to func.now() to
allow the database's own time function to be used.

> I was just wondering if there was an easy way to get around this issue
> without using MapperExtensions. Previously I was using
> MapperExtensions and overriding the before_update method which works
> since it looks for updates in the entire instance, not just that
> table. This way works fine, but I like the cleanness of not needing
> MapperExtensions.

the MapperExtension approach can be made more clean by creating a
generic one:

from sqlalchemy.orm import mapper as _mapper
from sqlalchemy.orm import MapperExtension

class MyExt(MapperExtension):
def before_update(self, mapper, connection, instance):
if hasattr(instance, '__before_update__'):
instance.__before_update__()

def mapper(*args, **kw):
kw['extension'] = MyExt()
return _mapper(*args, **kw)

just hide that code away someplace, and then any instance which
defines a method called __before_update__() will have it called before
update. A future release may include decorators which accomplish a
similar result.

a...@svilendobrev.com

unread,
Sep 2, 2008, 4:40:10 PM9/2/08
to sqlal...@googlegroups.com
this can be useful, especialy if some attempt is made to combine all
the (non-overlapping) funcs into one MapExt - and not chain many
simple ones. even in the plain usage, some optimisation over the
given list of mapExts can be done - to avoid walking the list at each
event - e.g. converting the list of MapExt-dict-of-handlers into dict
of { method:list-of-handlers } -- but i guess this is more suitable
for a fancy 'try this' section in the docs - the speedup may or may
not be noticeable.
Reply all
Reply to author
Forward
0 new messages