On Sep 29, 2010, at 9:43 AM, Richard Poettler wrote:

> Hi,
> 
> I am new to SqlAlchemy and like it very much.
> 
> I want to use it on a pretty acient database schema, wich has prefixed
> all column names with a unique prefix for that table.
> 
> E.g.:
> 
> address_table = Table('a_address', metadata,
>        Column('A_ID', Integer, primary_key=True),
>        Column('A_VAT_ID', Integer), # TODO: link entries
>        Column('A_C_ID', Integer),
>        Column('A_Company_A_ID', Integer),
>        Column('A_P_ID', Integer),
>        Column('A_LOC_ID', Integer,
> ForeignKey('loc_location.LOC_ID')),
>        Column('A_VC_ID', Integer),
>        Column('A_Descr', String(50)),
>        Column('A_Name', String(100)),
>        Column('A_Name2', String(100)),
>        Column('A_Department', String),
>        Column('A_Street', String),
>        Column('A_ZipCode', String(20)),
>        Column('A_City', String(50)),
>        Column('A_State', String(50)),
>        Column('A_PObox', String(20)),
>        Column('A_POboxZipCode', String(20)),
>        Column('A_POboxCity', String(50)),
>        Column('A_TelPrefix', String(30)),
>        Column('A_TelExt', String(10)),
>        Column('A_Tel2', String(50)),
>        Column('A_FaxPrefix', String(30)),
>        Column('A_FaxExt', String(10)),
>        Column('A_Fax2', String(50)),
>        Column('A_Mobile', String(50)),
>        Column('A_EMail', String(80)),
>        Column('A_VATID', String(30)),
>        Column('A_LastSeen', Date),
>        Column('A_Timestamp', DateTime),
>        )
> 
> When I now map a class to the table like:
> 
> class Address(object):
>    pass
> 
> mapper(Address, address_table)
> 
> It now does the expected and maps all the 'A_*' fields into the class.
> My question now is, whether it is possible, to automatically remove
> (or create additional properties) the 'A_' prefix of the created
> properties and transform the all to lowercase. So that e.g. a city or
> street property would be available and be mapped to the correspoinding
> databasefields.
> 
> Sorry, if this question seems noobish, but I just want to avoid
> redundant typing of all the fields...

so you just generalize on the technique at 
http://www.sqlalchemy.org/docs/orm/mapper_config.html#attribute-names-for-mapped-columns
 :

mapper(Address, address_table, properties=dict(
        (c.name[2:], c) for c in address_table.c
))


or you could assign a .key to each of your columns, to give it an in-python 
name:

Column('A_Tel2', String(50), key='tel2')
 ...

print t.c.tel2

-- 
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.

Reply via email to