> -----Original Message----- > From: sqlalchemy@googlegroups.com [mailto:sqlalch...@googlegroups.com] > On Behalf Of Andrey Semyonov > Sent: 10 September 2010 14:05 > To: sqlalchemy > Subject: [sqlalchemy] Python's reserved keywords as column names > > Sometimes there's a need to use table's column names such as 'from', > 'import' etc... In my case it's 'from' (that's from a log parser I > can't change it or the change is not very obvious). So I make a map: > > cleanup_warning_table = sa.Table("cleanup_warning", meta.metadata, > sa.Column('id', sa.types.Integer, > primary_key = True), > sa.Column('proc_id', > mysql.MSMediumInteger(unsigned = True)), > sa.Column('queue', > sa.types.Unicode(11)), > sa.Column('from', > sa.types.Unicode(255)), > sa.Column('to', > sa.types.Unicode(255)), > sa.Column('proto', > sa.types.Unicode(255)), > sa.Column('helo', > sa.types.Unicode(255)), > sa.Column('message', > mysql.MSMediumText), > sa.Column('datetime', > sa.types.DateTime) > ) > > class CleanupWarning(object): > pass > > sa.orm.mapper(CleanupWarning, cleanup_warning_table) > cleanup_warning_table.metadata.bind = create_engine(meta.engine, > pool_recycle = 600) > > That's OK. Now when I try to get some data of it: > >>> a = Session.query(CleanupWarning).limit(1).all() > >>> dir(a[0]) > ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', > '__getattribute__', '__hash__', '__init__', '__module__', '__new__', > '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', > '__sizeof__', '__str__', '__subclasshook__', '__weakref__', > '_sa_class_manager', '_sa_instance_state', 'datetime', 'from', 'helo', > 'id', 'message', 'proc_id', 'proto', 'queue', 'to'] > > I see the 'from' property available in the instance, though I can't > access to in by it's name rather than through __getattribute__ > explicit call. > > Is there any way to map a DB column name to some reasonable (or > settable explicitly) property name? >
Hi Andrey, See the section in the docs 'Attribute Names for Mapped Columns': http://www.sqlalchemy.org/docs/orm/mapper_config.html#attribute-names-fo r-mapped-columns Hope that helps, Simon -- 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.