SqlAlchemy get all strings (don't cast to boolean or datetime)

588 views Asked by At

How can I change the way that sqlalchemy cast the return values of my select, so it only returns strings, instead of Booleans or Datetime objects? I'm reading from a postgresql db.

For example I'm getting this:

>>>result = connection.execute("SELECT * FROM some_table").first()
>>>result
(123, 'some string field', True, datetime.datetime(2015, 06, ....))

When I would like to get this:

>>>result
('123', 'some string field', 'true', '2015-06-13....') 
1

There are 1 answers

1
van On BEST ANSWER

How about:

result = tuple([str(col_value) for col_value in result])

If you would like to conversion to happen on the DB side, you will have to perform cast(...) function on each column separately.