Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a constant-valued column to a SQLAlchemy query response?

Tags:

sqlalchemy

To get the equivalent of

select a, b from very_wide_table

in SQLAlchemy I can write

VeryWideTable.query().with_entities(VeryWideTable.a, VeryWideTable.b)

What would be the equivalent of

select a, b, 'hello' from very_wide_table

I tried add_columns but it wants a column object and I don't know how to specify a fixed string. I tried to add a fixed string to with_entities but that was interpreted as a column name.

like image 735
Ray Toal Avatar asked Mar 10 '23 12:03

Ray Toal


1 Answers

Use literal:

.with_entities(VeryWideTable.a, VeryWideTable.b, literal("hello"))
like image 71
univerio Avatar answered Apr 27 '23 02:04

univerio