|
| 1 | +# ADR-0003: UUID Surrogate Primary Key with v4/v5 Split |
| 2 | + |
| 3 | +Date: 2026-03-21 |
| 4 | + |
| 5 | +## Status |
| 6 | + |
| 7 | +Accepted |
| 8 | + |
| 9 | +## Context |
| 10 | + |
| 11 | +A primary key strategy was required for the `players` table. The |
| 12 | +candidates considered were: |
| 13 | + |
| 14 | +- **Auto-increment integer** — simple, compact, sequential; but leaks |
| 15 | + row count and insertion order to clients, and integer IDs are |
| 16 | + predictable, which can be a security concern for resource enumeration. |
| 17 | +- **UUID v4** — randomly generated, opaque, non-sequential; no |
| 18 | + information leaked; standard for API-created resources. |
| 19 | +- **UUID v7 / ULID** — time-ordered UUIDs; better index locality than |
| 20 | + v4 but add a dependency and are less universally supported. |
| 21 | +- **Natural key (`squad_number`)** — human-readable, but not stable |
| 22 | + across squads or seasons; unsuitable as a surrogate. |
| 23 | + |
| 24 | +An additional constraint was seeded data: the project ships 26 |
| 25 | +pre-seeded players that must have stable, reproducible primary keys |
| 26 | +across environments so that tests can reference them by ID. |
| 27 | + |
| 28 | +UUID v4 (random) cannot satisfy this: regenerating the seed would |
| 29 | +produce different IDs each time. UUID v5 (deterministic, derived from |
| 30 | +a namespace and a name) produces the same UUID for the same input, |
| 31 | +making seeded records reproducible. The project stores pre-computed |
| 32 | +UUID v5 constants directly in the seed scripts rather than deriving |
| 33 | +them at runtime from the squad number. |
| 34 | + |
| 35 | +## Decision |
| 36 | + |
| 37 | +We will use a UUID surrogate primary key stored as a hyphenated string |
| 38 | +(`HyphenatedUUID` custom SQLAlchemy type). API-created records receive |
| 39 | +a UUID v4 (random); migration-seeded records receive a UUID v5 |
| 40 | +(deterministic, pre-computed and stored as constants in the seed |
| 41 | +scripts). |
| 42 | + |
| 43 | +The v4/v5 split preserves the benefits of each approach in its context: |
| 44 | +randomness for API-created records (opaque, non-predictable), and |
| 45 | +determinism for seeded records (stable across environments, safe for |
| 46 | +test fixtures). |
| 47 | + |
| 48 | +The UUID is intentionally opaque to external clients. It is exposed |
| 49 | +only via `GET /players/{player_id}` and `POST /players/` responses. |
| 50 | +All mutation endpoints (PUT, DELETE) use `squad_number` as the |
| 51 | +identifier — see ADR-0004. |
| 52 | + |
| 53 | +## Consequences |
| 54 | + |
| 55 | +**Positive:** |
| 56 | +- UUIDs are opaque; no information about row count or insertion order |
| 57 | + is exposed to clients. |
| 58 | +- The v5/v4 split means seeded records have stable IDs across |
| 59 | + environments, safe to hard-code in tests. |
| 60 | +- `HyphenatedUUID` stores IDs as standard hyphenated strings in SQLite, |
| 61 | + readable without special tooling. |
| 62 | + |
| 63 | +**Negative:** |
| 64 | +- UUIDs are larger than integers (36 chars as strings), which has a |
| 65 | + minor storage and index performance cost — acceptable at PoC scale. |
| 66 | +- The v4/v5 distinction is non-obvious; developers unfamiliar with the |
| 67 | + codebase may not know why seeded records have deterministic IDs. |
| 68 | +- Clients cannot use the UUID to infer insertion order or paginate |
| 69 | + reliably by ID (UUID v4 is random). |
0 commit comments