From bf0e88d6fddc019191da043a50c5dffe26b65871 Mon Sep 17 00:00:00 2001 From: jakeross Date: Mon, 29 Sep 2025 10:24:07 -0600 Subject: [PATCH] fix: ensure sorting handles string columns correctly in query_helper.py --- services/query_helper.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/services/query_helper.py b/services/query_helper.py index 28935d47c..3f0e3dd24 100644 --- a/services/query_helper.py +++ b/services/query_helper.py @@ -18,7 +18,7 @@ from fastapi import HTTPException from fastapi_pagination.ext.sqlalchemy import paginate -from sqlalchemy import select, Float, Integer, Column, Select, func +from sqlalchemy import select, Float, Integer, Column, Select, func, String from sqlalchemy.orm import DeclarativeBase, Session from sqlalchemy.sql.elements import OperatorExpression from starlette.status import HTTP_404_NOT_FOUND @@ -130,7 +130,12 @@ def order_sort_filter( "Sort parameter is required when order is specified. " f"The sort parameter should be a column name in the table {table}." ) - attr = func.lower(getattr(table, sort)) + + attr = getattr(table, sort) + # test if column is a string col + if isinstance(attr.type, String): + attr = func.lower(attr) + if order.lower() == "asc": sql = sql.order_by(attr.asc()) elif order.lower() == "desc":