Skip to content Skip to sidebar Skip to footer

Select As In Sqlalchemy

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 = s

Solution 1:

Use the label(...) function: users_table.c.userid.label('NewColumn')

i.e.,

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

evaluates to:

SELECT username, userid as NewColumn From MyTable;

Solution 2:

Maybe literal_column?

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

See https://docs.sqlalchemy.org/en/13/core/sqlelement.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')])

Solution 3:

Post a Comment for "Select As In Sqlalchemy"