You need to reverse the order of your functions:
session.query(ResultsDBHistory).order_by(asc(func.lower(history_sort_order_column))).all()
so below and then declare an ascending order.
Alternatively, change the mapping to NOCASE :
from sqlalchemy.sql import collate session.query(ResultsDBHistory).order_by(asc(collate(history_sort_order_column, 'NOCASE'))).all()
which is perhaps the best idea.
I donβt think that ASC is required, as a result of which this simplifies the code:
from sqlalchemy.sql import collate session.query(ResultsDBHistory).order_by(collate(history_sort_order_column, 'NOCASE')).all()
source share