Skip to content

feat: UUIDv7 primary keys (Python 3.14 uuid7) - #12

Merged
JsNcAr merged 3 commits into
mainfrom
feat/uuid7-ids
Oct 8, 2026
Merged

JsNcAr merged 3 commits into
mainfrom
feat/uuid7-ids

Conversation

@JsNcAr

@JsNcAr JsNcAr commented Oct 8, 2026

Copy link
Copy Markdown
Owner

What this PR does

First of three PRs that use what Python 3.14 and the updated libraries make possible. This one adopts the single 3.14 feature with product impact on this code base: uuid.uuid7() primary keys.

Why

  • An auto-increment id tells anyone who sees one how many accounts exist and in what order they were created. A UUID does not.
  • Version 7 (RFC 9562) is time-ordered, so the primary-key index keeps appending at the end instead of scattering the way uuid4 would.
  • The token subject was already str(user.id), so the authentication model (id in the token, user loaded on every request, deletion effective at once) is unchanged in shape; only the type of the id moves.

What changes

  • User.id: uuid.UUID = Field(default_factory=uuid.uuid7, primary_key=True); the column compiles to PostgreSQL UUID.
  • UserResponseSchema.id is a UUID, serialised as a string.
  • get_current_user parses the subject with uuid.UUID(...); anything else keeps the fixed 401 Could not validate credentials.
  • get_user_by_id and the service's exclude_user_id take a UUID.
  • Tests: the two id comparisons now compare against str(test_user.id); a new test registers two users and checks both ids are version 7, distinct, and that the later one sorts after the earlier.
  • Docs: schemas, authentication, README; changelog entry marked breaking.

Breaking for an existing database

The users.id column type changes from integer to UUID. create_all creates missing tables and never alters a column, so a deployment with data must migrate first. The starter has no deployment; the changelog says this plainly.

Not changed

The other 3.14 features were evaluated and do not apply here (template strings, deferred annotations, multiple interpreters, free-threading, zstd); the plan and the next PRs record that so the question is not reopened.

Verified

  • black --check, flake8, mypy pass; 34 database-free tests pass; 75 collect.
  • By hand: the id column compiles to UUID for PostgreSQL, a new User() gets a version-7 id, the response schema serialises it as a string, and a token subject round-trips to the same UUID.
  • The database-backed tests (including deleted-user and non-UUID-subject refusals, and the new ordering test) run in this PR's test job.

🤖 Generated with Claude Code

JsNcAr and others added 3 commits October 7, 2026 23:29
User.id is a uuid.UUID generated by uuid.uuid7() (Python 3.14, RFC 9562)
instead of an auto-increment integer. An id no longer tells anyone who sees
one how many accounts exist or in what order they were created, and version 7
is time-ordered, so the primary-key index still appends at the end rather
than scattering as uuid4 would. The token subject was already str(user.id);
get_current_user now parses it as a UUID and keeps the fixed 401 for anything
else. Responses carry the id as a UUID string.

Breaking for an existing database: the users.id column type changes and
create_all never alters a column; migrate before upgrading a deployment with
data.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@JsNcAr
JsNcAr merged commit ab08501 into main Oct 8, 2026
2 checks passed
@JsNcAr
JsNcAr deleted the feat/uuid7-ids branch October 8, 2026 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant