Andrey Semyonov
unread,Sep 10, 2010, 9:04:41 AM9/10/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sqlalchemy
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?