Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy add label to static string in select

this is my query:

select 'somestring' as my_col_name
from my_table

when using sql alchemy:

query = db.session.query(
  'somestring'.label('my_col_name'))
)

but I get this error:

'str' object has no attribute 'label'

how can I add a label on sqlalchemy to a static string field? (I need it for some unions I do after)

like image 353
dina Avatar asked Nov 19 '25 07:11

dina


1 Answers

use literal_column

query = db.session.query(
  literal_column("'somestring'").label('my_col_name'))
)
like image 192
dina Avatar answered Nov 21 '25 19:11

dina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!