Python's reserved keywords as column names

2,739 views
Skip to first unread message

Andrey Semyonov

unread,
Sep 10, 2010, 9:04:41 AM9/10/10
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?

King Simon-NFHD78

unread,
Sep 10, 2010, 9:15:05 AM9/10/10
to sqlal...@googlegroups.com

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

Andrey Semyonov

unread,
Sep 10, 2010, 9:35:24 AM9/10/10
to sqlalchemy
On 10 сен, 17:15, "King Simon-NFHD78" <simon.k...@motorola.com> wrote:
> 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

Well, this leads to the only way to map in my case named
'Declarative'. Because it would fail on

mapper(Class, table, properties = { '_from': table.c.from })

Could non-declarative way for mapping python's reserved keywords as
column names be scheduled as a bug or enhancement request ?

King Simon-NFHD78

unread,
Sep 10, 2010, 10:32:31 AM9/10/10
to sqlal...@googlegroups.com
> -----Original Message-----
> From: sqlal...@googlegroups.com [mailto:sqlal...@googlegroups.com]
> On Behalf Of Andrey Semyonov
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.

The 'c' collection on a Table object allows dictionary-style access, so you should be able to use:

mapper(Class, table, properties = { '_from': table.c['from'] })

Even if that didn't work, you could always use Python's getattr function:

mapper(Class, table, properties = { '_from': getattr(table.c, 'from') })

Hope that helps,

Simon

Michael Bayer

unread,
Sep 10, 2010, 10:36:21 AM9/10/10
to sqlal...@googlegroups.com

Option 1:

mapper(C, t1, properties={'from_':t1.c['from']})

Option 2:

t1 = Table('sometable', metadata, Column('from', Integer, key='from_'))
t1.c.from_

these techniques are documented at:

http://www.sqlalchemy.org/docs/core/schema.html#accessing-tables-and-columns
http://www.sqlalchemy.org/docs/core/schema.html#sqlalchemy.schema.Column

Andrey Semyonov

unread,
Sep 13, 2010, 2:09:12 AM9/13/10
to sqlalchemy
Thanks to all of you. Dict-style mapping declaration helped.
Reply all
Reply to author
Forward
0 new messages