2

I have a sqlalchemy query in my flask app like

results = db.session.query(
 Data.id.label('id'),
 Data.timestamp.label('timestamp'),
 ...
).all()

on which I want to add a string value. Is there a way to add that in the query, like

results = db.session.query(
 Data.id.label('id'),
 Data.timestamp.label('timestamp'),
 'foo'.label('bar')
).all()

?

My current solution is to loop over all the results, convert them to dicts and add the string

results_ = []
for r in results:
 r_ = {}
 for k in result.keys():
  r_[k] = r[k]
 r_['bar'] = 'foo'
 results_.append(r_)

but it doesn't seem like the pythonic way of doing this.

Solution:

results = db.session.query(
 Data.id.label('id'),
 Data.timestamp.label('timestamp'),
 literal('foo').label('bar')
).all()

0 Answers0