Skip to content

feat: Option to use UUIDs as primary keys #3255

Description

@marcelomendoncasoares

Problem to Solve

This issue proposes a comprehensive feature of how to allow the usage of UUID as primary keys instead of int. The feature is already discussed and implemented in the draft PR #3152. However, as the PR touches large parts of the serverpod code, we will break it into several small PRs that will slowly incorporating the feature into the framework. All such PRs will reference this issue.

For reference, the feature is a recurrent request that was previously discussed on the issues #351, #635 and #2641. It is also a pre-requisite for the issue #649.

Proposal

The proposed implementation should:

  • Allow the user to declare the id field type in serverpod models, changing its type to one of the supported id types (with the correct default attribution). The int type should not need default values, although it can be optionally specified. Any relation to the model will use this id as FK if needed.
  • Expose an alternative to change the default id type for the project. This will make all tables be generated with the chosen option instead of the default int type. The option will be persisted in the generator.yaml config file for the language server and subsequent calls to serverpod generate be able to interpret the project correctly.
  • Add a new default option for UuidValue fields to create UUIDs randomly according to the v7 spec, that solves most performance issues of using UUIDs as PK (lack of PK sorting, increased storage, index bloat, etc.) by generating lexicographically sortable ids. This option should be supported as a valid id type.

Use Case

Although the common best practice is to use sequential as PK, there are some use cases where using UUIDs is preferred. In distributed systems, for example, the main advantage is the possibility to create several related entities on the client and feed them to the server without round-trips to get ID's. On offline-first applications, in which local and remote database must be a copy of each other, using UUIDs as PK also simplifies upserts without dealing with unique indexes violations (specially for related entities).

Alternatives

In the referenced discussions, it is suggested to create unique indexes on UUID fields instead of using it as PK, but this only ensures uniqueness through constraints, while relationships between entities remain on sequential fields.

Additional context

Additional context regarding the proposal can be found in the discussion of the PR #3152.

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    area: models
    on Feb 24, 2025
  2. SandPod commented on Apr 8, 2025

    @SandPod
    Contributor

    Fixed by: #3299

  3. moved this from Now 🔥 to Done 🚀 in Serverpod Frameworkson Apr 8, 2025
  4. marcelomendoncasoares commented on Apr 10, 2025

    @marcelomendoncasoares
    CollaboratorAuthor

    From the complete spec on the proposal, we still have two features pending from this issue:

    • Expose an alternative to change the default id type for the project.
    • Add a new default option for UuidValue fields to create UUIDs randomly according to the v7 spec.

    Should we keep the issue closed since the core feature is already implemented - but reference it anyways in the next PRs -, or is it better leave it open to signal that it is still in progress?

  5. moved this from Done 🚀 to Now 🔥 in Serverpod Frameworkson Apr 10, 2025
  6. SandPod commented on Apr 10, 2025

    @SandPod
    Contributor

    Sorry, I was a bit trigger happy. Let's keep this issue open until the subtasks are completed.

  7. tananga commented on May 24, 2025

    @tananga

    Great feature guys 🥳

  8. SandPod commented on Jun 17, 2025

    @SandPod
    Contributor

    @marcelomendoncasoares

    Expose an alternative to change the default id type for the project.

    A separate issue has been created to track this: #3665

  9. moved this from Now 🔥 to Done 🚀 in Serverpod Frameworkson Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: modelsenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions