On Tuesday 02 September 2008 22:50:09 Michael Bayer wrote:
> 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_map
> >per_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.
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.

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to