Skip to content

fix: Allow only defaultPersist and defaultModel keys on id field - #3416

Merged
SandPod merged 12 commits into
serverpod:mainfrom
marcelomendoncasoares:fix-change-id-type-default
Apr 16, 2025
Merged

SandPod merged 12 commits into
serverpod:mainfrom
marcelomendoncasoares:fix-change-id-type-default

Conversation

@marcelomendoncasoares

@marcelomendoncasoares marcelomendoncasoares commented Apr 14, 2025 •

Copy link
Copy Markdown
Collaborator

As discussed on #3396, this PR removes the possibility of the user to declare an id field using the default keyword. This forces the user to decide whether to have UUID values being defined on the model (using the defaultModel keyword) or only on the database (using the defaultPersist keyword).

While making such change, it became clear that we were allowing the user to declare the type as non-nullable, but the resulting type was always set as nullable (as required by the TableRow.id model). This is now fixed, with the user being required to declare the id type as nullable (such as already happens when declaring foreign key fields).

Closes: #3396

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

No breaking changes.

@tp

tp commented Apr 14, 2025 •

Copy link
Copy Markdown
Contributor

This is now fixed, with the user being required to declare the id type as nullable (such as already happens when declaring foreign key fields).

@marcelomendoncasoares Should a UUID with defaultModel ideally not end up with a non-null id field? Because it will always be set on the model, and when setting it null it lacks any information of what the database should do, so it would fail to insert, no?

Personally I would also think this is nicer for the "notes use-case" described somehwere, where you want to link UUIDs locally before having them persisted. In that case it'd be very nice to use model.id instead of model.id! and having to note, that for this class it'll be fine (as the id is always generated with the model).

Just my 2 cents, maybe you and @SandPod found some good reasons why we could not support opt-in non-null IDs even with a default model value.

Edit: I think there is also always the slight mismatch in the types / behaviors anyway, in that an int? in id and an int? in any other relationship field do not mean the same thing, because at least after an INSERT or SELECT the id will always be set, whereas the relationship defined that way is truly optional and might be null. So maybe this a distinction we can not currently express.

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator Author

@marcelomendoncasoares Should a UUID with defaultModel ideally not end up with a non-null id field? Because it will always be set on the model, and when setting it null it lacks any information of what the database should do, so it would fail to insert, no?

Personally I would also think this is nicer for the "notes use-case" described somehwere, where you want to link UUIDs locally before having them persisted. In that case it'd be very nice to use model.id instead of model.id! and having to note, that for this class it'll be fine (as the id is always generated with the model).

I agree with you and even tried this as first implementation, but had to revert it (note the commit #45d6efc) due to the abstract TableRow class having the id field as nullable. To allow having a non-nullable id field, we would need to pass the nullable type to the generic type of the TableRow (instead of only the type as we do today). Something like this:

/// Holds data corresponding to a row in the database. Concrete classes are
/// typically generated. Instances of [TableRow] can also be serialized and
/// either passed to clients or cached.
abstract interface class TableRow<T_ID> implements SerializableModel {
  TableRow(this.id);  // <-- Add a constructor due to the id field being possibly non-nullable

  /// The id column of the row. Can be null if this row is not yet stored in
  /// the database.
  T_ID id;  // <-- Remove the '?' from here

  /// The table that this row belongs to.
  Table<T_ID> get table;
}

And change the generator to pass the type as nullable on each model, like this:

abstract class AuthKey
    implements _i1.TableRow<int?>, _i1.ProtocolSerialization {  // <-- Add the nullable '?' to "int" here
  AuthKey._({
    this.id,
    required this.userId,
    required this.hash,
    this.key,
    required this.scopeNames,
    required this.method,
  });

This, however simple, is a breaking change. It will require re-generating all table class models (including the ones on auth and chat modules). So I though it would be better not to couple the change to this PR, since we could treat "enable non-nullable id field" as a separate feature. But I would have no problem adding it to this PR if we understand it to be better.

@SandPod

SandPod commented Apr 15, 2025

Copy link
Copy Markdown
Contributor

This, however simple, is a breaking change. It will require re-generating all table class models (including the ones on auth and chat modules).

I think this change would be OK. Since we have a strict version requirement all serverpod dependencies this "generate" change would be reflected as soon as you upgrade your project.

To me, it sounds like basically the same change as when we went from TableRow to TableRow<T_ID>. This also touches all models in the project. And we are only connecting it to new features, so old Id models would always still be nullable with the current configuration.

This STILL is probably best introduced in a separate PR, non-nullable id field if defaultModel is set. We can have this PR as an intermediary step.

@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 work!

Would you like to explore the possibility to remove nullability when defaultModel is defined in this PR or in a separate one? Otherwise all changes here look good except for some minor questions. 👍

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator Author

Would you like to explore the possibility to remove nullability when defaultModel is defined in this PR or in a separate one?

I also prefer to tackle it in a separate PR, to keep the fix here well scoped. The change is simple and is already tested, but I think we will be able to validate it better separately.

@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.

LGTM! 👍

Single suggestion to fix in the test then we an merge it.

@SandPod

SandPod commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

Created an issue for the non-nullable id field: #3429

@marcelomendoncasoares If you comment on it I can assign it to you.

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator Author

@SandPod, I see we have a failing test, but it is not related to the change in the last commit, right? It happened yesterday between pushes as well, and disappeared after a new run. Locally, all tests are passing normally, such as with most of the CI runs. Is it a known bug that the test_integration/shutdown_test.dart is sometimes failing?

@SandPod

SandPod commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

@marcelomendoncasoares - No, this is a flaky test in main. I will disable it and file an issue for it.

@SandPod
SandPod merged commit 0b666ec into serverpod:main Apr 16, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 16, 2025
@marcelomendoncasoares
marcelomendoncasoares deleted the fix-change-id-type-default branch April 16, 2025 22:54
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.

UUID id fields get a value by default

3 participants