kslnet
kslnet

Reputation: 574

In SQLAlchemy Core, how can I create a table with a column name that differs from its attribute?

I'm creating a bunch of simple tables like this:

newtable = Table("mytable", metadata, Column('user_id', Integer), Column('value', Integer))

I know I can access the user_id column like newtable.c.user_id. However, the name of this column differs among tables, e.g. 'program_id' or 'credit_id'. I want to be able to create the table such that I can access this first column by a generic key, say 'id', and then use the column attributes to get its actual name, i.e.

column_name = newtable.c.id.name

and then column_name = 'user_id'.

How do I modify my table creation to do this? I know how to do it if I'm using declarative_base in the ORM, but I haven't been able to find anything in the docs to do this if I'm just using the Expression Language.

Upvotes: 0

Views: 182

Answers (1)

van
van

Reputation: 77082

Just use key parameter:

Column('user_id', Integer, key="id")

Upvotes: 1

Related Questions