views:

42

answers:

2

I want to do something like this:

select username, userid, 'user' as new_column  from  users_table.

The columns of the table can be selected using sqlalchemy as follows:

query = select([users_table.c.username, users_table.c.userid])

How do I do the select x as col_x to the query in sqlalchemy?

+2  A: 

See the alias() in here: http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/expressions.html

Radomir Dopieralski
+1  A: 

Maybe literal_column?

query = select([users_table.c.username, users_table.c.userid, literal_column("user", type_=Unicode).label('new_column')])

See http://www.sqlalchemy.org/docs/reference/sqlalchemy/expressions.html#sqlalchemy.sql.expression.literal_column

Edit: actually I should have said "literal":

query = select([users_table.c.username, users_table.c.userid, literal("user", type_=Unicode).label('new_column')])
literal_column() is more appropriate here unless the user wants a bind parameter to be generated. so your first answer is correct.
zzzeek