Skip to content

refactor: Enable table primary keys to have other types than int - #3257

Merged
SandPod merged 12 commits into
serverpod:mainfrom
marcelomendoncasoares:generic-table-class
Mar 10, 2025
Merged

SandPod merged 12 commits into
serverpod:mainfrom
marcelomendoncasoares:generic-table-class

Conversation

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator

This PR enables the feature #3255 implementation by removing the core premise of the serverpod package that table ids are exclusively of int type. To do this, the following changes were made:

  • Base column types were rearranged and there is a new public column type ComparableColumn<T>. This is the type of valid id columns and implements equality and inequality comparison. All types that have such comparison options on postgres are now ComparableColunn. With this, the string and uuid types can now be compared using less than/more than (and the equal to variant) operators, as supported by Postgres.
  • The Table and TableRow concept classes are now generic to the type of the ID attribute, with the TableRow<T_ID>.id being of type ComparableColunn<T_ID>. On the Database level and below, references to the id column were changed to Object instead of int and all server models were re-generated.
  • The ObjectRelationDefinition and ListRelationDefinition objects now store the id type of the table that owns the FK field. This reference is used to pass the T_ID generic reference to build the attach/detach repository methods on the server models.

The PR contains tests of the changes, with an exception of one test that will be added on a subsequent PR of the feature #3255: the test that the stored FK field type on the relation objects is being collected from the correct model. The test exists on the #3152 draft PR, but it requires that more than one type of id is already implemented. Otherwise, it is not be possible to assert that the collected int type comes from one or other model of the relation.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

After the PR, users will have to run serverpod generate again to update the models. However, it is already part of the update routine and not considered a breaking change.

@SandPod

SandPod commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

Awesome work! Will take a look at this first thing tomorrow morning! 👍

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clean PR!

Had a couple of comments and suggestions.

We might want to hold off a bit while merging this to get the 3.4 release out of the way.

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good now! Have a last possible change to ensure that we don't add more coupling between our columns class and the sql query builder.

We want to ensure we don't forget about the type, but we don't necessarily want to build (more) sql strings outside of the sql query generator.

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me! Nice diligent work! 🚀

@SandPod
SandPod merged commit 751efbb into serverpod:main Mar 10, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 10, 2025
@marcelomendoncasoares
marcelomendoncasoares deleted the generic-table-class branch March 11, 2025 17:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants