From 4c25d91e185603ac8e0c7e99b1bd9e34fb2b41b7 Mon Sep 17 00:00:00 2001 From: Conrad Date: Thu, 25 Apr 2024 19:43:12 +0200 Subject: [PATCH] fix: fixed another minor bug with the order_by method --- CHANGELOG.md | 4 ++++ creyPY/fastapi/order_by.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f349aa7..0371073 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## 1.2.X + +- Added order_by method + ## 1.1.0 - Added headers to testing diff --git a/creyPY/fastapi/order_by.py b/creyPY/fastapi/order_by.py index 3238893..53385e9 100644 --- a/creyPY/fastapi/order_by.py +++ b/creyPY/fastapi/order_by.py @@ -1,7 +1,8 @@ from typing import Callable from pydantic.json_schema import SkipJsonSchema -from sqlalchemy import asc, desc +from sqlalchemy import String, asc, cast, desc +from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.sql.selectable import Select @@ -9,7 +10,16 @@ def order_by(order_by: str | SkipJsonSchema[None] = None) -> Callable[[Select], def _order_by(query: Select) -> Select: if order_by: direction = desc if order_by.startswith("-") else asc - query = query.order_by(direction(order_by.lstrip("-"))) + column_name = order_by.lstrip("-") + + # Get the column from the query + for column in query.inner_columns: + if column.key == column_name: + # If the column is a UUID, cast it to a string + if isinstance(column.type, UUID): + column = cast(column, String) + query = query.order_by(direction(column)) + break return query return _order_by