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.
This commit is contained in:
Cal Corum 2026-01-31 16:06:44 -06:00
parent 7574e488d9
commit 985a6ed2b0
2 changed files with 18 additions and 0 deletions

View File

@ -178,6 +178,17 @@ class BaseModel(Model):
class Meta: class Meta:
database = db 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): class Current(BaseModel):
season = IntegerField() season = IntegerField()

View File

@ -1,4 +1,11 @@
import math 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 datetime import datetime
from typing import List from typing import List
import logging import logging