From 985a6ed2b00a67cbc9f6a5c1a8d2a55a7be6e96b Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sat, 31 Jan 2026 16:06:44 -0600 Subject: [PATCH] Add default ORDER BY id to BaseModel.select() for PostgreSQL compatibility PostgreSQL does not guarantee row order without ORDER BY, unlike SQLite which implicitly returned rows by rowid. This caused bugs where queries returned results in unexpected order (e.g., get_team_by_owner returning gauntlet team instead of main team). Override select() in BaseModel to add default ordering by id. Explicit .order_by() calls will override this default. Also mark legacy db_engine.py as deprecated. --- app/db_engine.py | 11 +++++++++++ db_engine.py | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/app/db_engine.py b/app/db_engine.py index 85f3f86..8c587ea 100644 --- a/app/db_engine.py +++ b/app/db_engine.py @@ -178,6 +178,17 @@ class BaseModel(Model): class Meta: database = db + @classmethod + def select(cls, *fields): + """Override select to add default ordering by id for PostgreSQL compatibility. + + PostgreSQL does not guarantee row order without ORDER BY, unlike SQLite + which implicitly returned rows by rowid. This ensures consistent ordering + across all queries unless explicitly overridden with .order_by(). + """ + query = super().select(*fields) + return query.order_by(cls.id) + class Current(BaseModel): season = IntegerField() diff --git a/db_engine.py b/db_engine.py index 03c885b..ebd3046 100644 --- a/db_engine.py +++ b/db_engine.py @@ -1,4 +1,11 @@ import math + +""" +DEPRECATED: This file is a legacy implementation from before the deployment +of /database/app/. The active codebase is now in /database/app/db_engine.py. +This file is kept for reference only and should not be used. +""" + from datetime import datetime from typing import List import logging