Re: [sqlalchemy] Re: onupdate Column attribute with joined table inheritance question
btw, using the __before_update__ technique introduced some weird data corruption issues for me when my class had PickleType datamembers. For instance, # Create the Foo class to map foos_table to class Foo(object): def __before_update__(self): self.some_pickle_type self.last_edit_date = datetime.now() the sql update statement generated would set some_pickle_type to NULL, even though I hadn't modified the datamember. This doesn't matter hugely to me at the moment, because I no longer am using a table hierarchy, and hence don't need to use __before_update__ anymore, but thought I would mention it in case anyone else runs into it. sqlalchemy.__version__ '0.5.4p2' On Mon, Mar 2, 2009 at 12:43 PM, John Fries john.a.fr...@gmail.com wrote: Thanks Michael! That should have been obvious to me, but for some reason I couldn't figure it out. On Mon, Mar 2, 2009 at 11:24 AM, Michael Bayer mike...@zzzcomputing.comwrote: On Mar 2, 2009, at 2:16 PM, John Fries wrote: Hi all, I am having a problem that is identical to the one mentioned last year on this list: http://groups.google.com/group/sqlalchemy/browse_thread/thread/efd3993c94c8d162/37943cda02151f2b?lnk=gstq=onupdate+inheritance#37943cda02151f2b I have Engineer and Manager tables that are inheriting from an Employee table using joined table inheritance. When I update one of the child tables, I want the last_edited timestamp column on the Employee table to be updated as well, but it is not updating. Mr. Bayer proposes the following: 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. I've done that step, but I don't understand what the next step is. I thought that I would have to implement a __before_update__ method on my Engineer and Manager classes, which would then call some method on Employee using super. However, when I update my Engineer, it doesn't even look like the __before_update__ method is being called. So I conclude that I'm doing it wrong.Which instances should implement a __before_update__ method? What should the __before_update__ method do? just implement def __before_update__() on your base Employee class. this method should set the last_edited attribute to a new value (note you can set it to func.now() to have a SQL function fire off). Engineer and Manager will have the method automatically via class inheritance. Also make sure you are using the modfied mapper() function to create your mappers. --~--~-~--~~~---~--~~ 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.comsqlalchemy%2bunsubscr...@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 sqlalch...@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.
[sqlalchemy] Re: onupdate Column attribute with joined table inheritance question
On Mar 2, 2009, at 2:16 PM, John Fries wrote: Hi all, I am having a problem that is identical to the one mentioned last year on this list: http://groups.google.com/group/sqlalchemy/browse_thread/thread/efd3993c94c8d162/37943cda02151f2b?lnk=gstq=onupdate+inheritance#37943cda02151f2b I have Engineer and Manager tables that are inheriting from an Employee table using joined table inheritance. When I update one of the child tables, I want the last_edited timestamp column on the Employee table to be updated as well, but it is not updating. Mr. Bayer proposes the following: 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. I've done that step, but I don't understand what the next step is. I thought that I would have to implement a __before_update__ method on my Engineer and Manager classes, which would then call some method on Employee using super. However, when I update my Engineer, it doesn't even look like the __before_update__ method is being called. So I conclude that I'm doing it wrong.Which instances should implement a __before_update__ method? What should the __before_update__ method do? just implement def __before_update__() on your base Employee class. this method should set the last_edited attribute to a new value (note you can set it to func.now() to have a SQL function fire off). Engineer and Manager will have the method automatically via class inheritance. Also make sure you are using the modfied mapper() function to create your mappers. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: onupdate Column attribute with joined table inheritance question
Thanks Michael! That should have been obvious to me, but for some reason I couldn't figure it out. On Mon, Mar 2, 2009 at 11:24 AM, Michael Bayer mike...@zzzcomputing.comwrote: On Mar 2, 2009, at 2:16 PM, John Fries wrote: Hi all, I am having a problem that is identical to the one mentioned last year on this list: http://groups.google.com/group/sqlalchemy/browse_thread/thread/efd3993c94c8d162/37943cda02151f2b?lnk=gstq=onupdate+inheritance#37943cda02151f2b I have Engineer and Manager tables that are inheriting from an Employee table using joined table inheritance. When I update one of the child tables, I want the last_edited timestamp column on the Employee table to be updated as well, but it is not updating. Mr. Bayer proposes the following: 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. I've done that step, but I don't understand what the next step is. I thought that I would have to implement a __before_update__ method on my Engineer and Manager classes, which would then call some method on Employee using super. However, when I update my Engineer, it doesn't even look like the __before_update__ method is being called. So I conclude that I'm doing it wrong.Which instances should implement a __before_update__ method? What should the __before_update__ method do? just implement def __before_update__() on your base Employee class. this method should set the last_edited attribute to a new value (note you can set it to func.now() to have a SQL function fire off). Engineer and Manager will have the method automatically via class inheritance. Also make sure you are using the modfied mapper() function to create your mappers. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: onupdate Column attribute with joined table inheritance question
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. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: onupdate Column attribute with joined table inheritance question
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 -~--~~~~--~~--~--~---