Relational modeling & primary keys (UUID v4 vs v7)
Tables, rows, keys, relationships and cardinality. Surrogate vs natural keys, and why SperoTek uses UUIDv7 while the others use random UUIDs.
The concept
A relational database stores data in tables (relations) of rows (tuples) with typed columns. Relationships are expressed with foreign keys:
| Cardinality | Example in your projects | How it’s stored |
|---|---|---|
| One-to-one | User ↔ Profile (Store) | FK with UNIQUE, or a shared primary key |
| One-to-many | Hostel → Rooms (Hostel), JournalEntry → JournalLines (SperoTek) | FK on the “many” side (rooms.hostel_id) |
| Many-to-many | User ↔ Role (RM), User ↔ wishlist Products (Store) | A join table (user_roles, wishlist) |
| Self-referencing | ReportUnit → parent ReportUnit (RM) | FK to the same table (parent_id) |
Primary keys
- A natural key is a real-world value (email, ISBN). It’s tempting, but real-world values change. Users change their email.
- A surrogate key is meaningless and system-generated. All your projects use surrogate keys.
Which surrogate?
| Type | Pros | Cons |
|---|---|---|
BIGSERIAL / identity |
Small (8 bytes), ordered, fast inserts | Guessable (/orders/1001), must come from the DB, hard to merge across systems |
| UUIDv4 (random) | Not guessable, can be generated in the app, globally unique | 16 bytes; random order fragments B-tree indexes |
| UUIDv7 (time-ordered) | Not guessable, generated in the app, sortable by creation time, index-friendly | Leaks the creation timestamp |
| ULID / Snowflake | Similar to v7 | Non-standard |
Why randomness hurts: B-tree indexes keep keys sorted. Random v4 inserts land on arbitrary leaf pages, which causes page splits, a poor cache hit rate and write amplification. v7 values always append to the rightmost page, just like a sequence.
In your projects
shared/domain/BaseEntity.java@Id
@UuidGenerator(style = UuidGenerator.Style.VERSION_7)
private UUID id;
@CreationTimestamp private Instant createdAt;
@UpdateTimestamp private Instant updatedAt;A ledger table grows forever, so insert-friendly keys matter. v7 also gives a rough “created order” for free.
shared/domain/BaseEntity.javaUUID id (@Id @UuidGenerator, random), timestamps, and soft-delete columns is_deleted and deleted_at, inherited by about 27 entities. ReportUnit is a self-referencing tree with parent and Set<ReportUnit> subunits.
db/script/initial-schema.sql · entities/Profile.javaGenerated in the DB with gen_random_uuid(). Profile uses a shared primary key with User via @MapsId, so profiles.id is users.id. That is the tightest form of one-to-one.
initial-schema.sqlThe uuid-ossp extension and @UuidGenerator. It also has a natural uniqueness rule alongside the surrogate key: UNIQUE (hostel_id, room_number), so room “A12” can exist in two hostels but only once per hostel.
Related concepts you must know
- Candidate keys and alternate keys. Every column set that could be the key (e.g. email) should still get a UNIQUE constraint.
- Composite keys. SperoTek’s
UNIQUE (company_id, id)exists so that composite foreign keys can enforce tenant isolation (see the constraints lesson). - ER diagrams. Learn to sketch entities, attributes and crow’s-foot cardinality before writing SQL.
- Hiding IDs isn’t authorization. UUIDs make guessing hard, but you still need ownership checks (Store’s
order.isPlacedBy(user)).