From c491cc4dd05365b518bbd440ca402738f002633d Mon Sep 17 00:00:00 2001 From: Senko Rasic Date: Tue, 4 Jun 2024 09:47:55 +0200 Subject: [PATCH] don't use MAX on an UUID field --- core/db/models/project.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/db/models/project.py b/core/db/models/project.py index bf70dc163..4f1a716aa 100644 --- a/core/db/models/project.py +++ b/core/db/models/project.py @@ -4,7 +4,7 @@ from unicodedata import normalize from uuid import UUID, uuid4 -from sqlalchemy import delete, inspect, select +from sqlalchemy import and_, delete, inspect, select from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import Mapped, mapped_column, relationship, selectinload from sqlalchemy.sql import func @@ -79,7 +79,7 @@ async def get_all_projects(session: "AsyncSession") -> list["Project"]: from core.db.models import Branch, ProjectState latest_state_query = ( - select(ProjectState.branch_id, func.max(ProjectState.id).label("max_id")) + select(ProjectState.branch_id, func.max(ProjectState.step_index).label("max_index")) .group_by(ProjectState.branch_id) .subquery() ) @@ -88,7 +88,13 @@ async def get_all_projects(session: "AsyncSession") -> list["Project"]: select(Project, Branch, ProjectState) .join(Branch, Project.branches) .join(ProjectState, Branch.states) - .join(latest_state_query, ProjectState.id == latest_state_query.columns.max_id) + .join( + latest_state_query, + and_( + ProjectState.branch_id == latest_state_query.columns.branch_id, + ProjectState.step_index == latest_state_query.columns.max_index, + ), + ) .options(selectinload(Project.branches), selectinload(Branch.states)) .order_by(Project.name, Branch.name) )