--- id: 478cd8a2-efb4-47c2-a215-dcb95f486cf7 type: solution title: "PostgreSQL migration: Add default ORDER BY id to BaseModel.select()" tags: [paper-dynasty, postgresql, migration, solution, peewee, ordering] importance: 0.9 confidence: 0.8 created: "2026-01-31T22:06:36.571809+00:00" updated: "2026-01-31T22:06:36.571809+00:00" --- PostgreSQL does not guarantee row order without ORDER BY, unlike SQLite which implicitly returned rows by rowid. This caused bugs like get_team_by_owner returning the wrong team. Solution: Override select() in BaseModel to add default .order_by(cls.id). Explicit .order_by() calls will override this default. Tested with joins, where, limit, aggregations - all work correctly.