Repository navigation
Description
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
idfield type in serverpod models, changing its type to one of the supported id types (with the correct default attribution). Theinttype 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
inttype. The option will be persisted in thegenerator.yamlconfig file for the language server and subsequent calls toserverpod generatebe able to interpret the project correctly. - Add a new default option for
UuidValuefields 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
SandPod commentedon Apr 8, 2025
Fixed by: #3299
marcelomendoncasoares commentedon Apr 10, 2025
From the complete spec on the proposal, we still have two features pending from this issue:
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?
SandPod commentedon Apr 10, 2025
Sorry, I was a bit trigger happy. Let's keep this issue open until the subtasks are completed.
tananga commentedon May 24, 2025
Great feature guys 🥳
SandPod commentedon Jun 17, 2025
@marcelomendoncasoares
A separate issue has been created to track this: #3665