Repository navigation
refactor: Make id == null checks in attach/detach repositories conditional on id nullability - #5318
realmeylisdev wants to merge 3 commits into
Conversation
…itional on id nullability The generated attach/detach repository methods unconditionally emitted `if (x.id == null) throw ArgumentError.notNull(...)` guards, which are dead code when the checked model's id field is non-nullable (UUID ids with a defaultModel default). The guards were masked by adding a file-wide `ignore_for_file: unnecessary_null_comparison, dead_code` to every relation-bearing generated file. Each guard is now emitted only when the checked model's id is nullable, decided per side of the relation: - the enclosing model's id via `classDefinition.idField.type.nullable`, - the referenced model's id via a new `ObjectRelationDefinition.referenceIdType` populated by the resolver (the generator never sees module class definitions, so this must be resolved at analysis time), - list-relation element ids via `ListRelationDefinition.foreignKeyOwnerIdType`, fixing the implicit branch which stored the declaring class's id type instead of the foreign-key owner's (contradicting its own doc; the property was write-only in the generator, so this was unobservable). The relation-field null check in foreign-side detachRow is kept unconditionally since object-relation fields are always validated to be nullable. The `hasRelationWithNonNullableIds` lint-suppression workaround is removed. Closes serverpod#3462
Regenerated via util/generate_all. All relation-bearing generated files lose the `ignore_for_file: dead_code, unnecessary_null_comparison` header; files referencing models with non-nullable ids additionally lose the now-unreachable guard blocks.
|
The two red checks are not analysis findings. The windows-latest job was then canceled by the matrix fail-fast ("The operation was canceled."). All other checks are green, including |
|
Why 150 files: only 7 are the actual change (commit 76f4bdd — generator/analyzer + tests). The other 143 are regenerated goldens (commit abd96de, zero insertions, 250 deletions): the old workaround stamped the |
Since #3437, models can declare non-nullable id fields (UUID ids with a
defaultModeldefault). The generated attach/detach repository methods still unconditionally emittedif (x.id == null) { throw ArgumentError.notNull('x.id'); }guards, which are dead code for non-nullable ids and trigger theunnecessary_null_comparisonlint (dead_codeon Dart 3.10+). This was masked by adding a file-wideignore_for_fileto every relation-bearing generated file.This PR makes the guards conditional on the static nullability of the checked model's id, decided per side of the relation, and removes the
hasRelationWithNonNullableIdsworkaround as requested in the issue:classDefinition.idField.type.nullable(the attach/detach builder chain now takesModelClassDefinition, which is what the call site already passes).ObjectRelationDefinition.referenceIdType, populated by the dependency resolver. This has to happen at analysis time: the resolver has the referenced class definition in scope (including module models, e.g.module:auth:UserInfo), while the generator pipeline only ever receives project and shared models.ListRelationDefinition.foreignKeyOwnerIdType. The implicit-relation branch stored the declaring class's id type instead of the foreign-key owner's, contradicting the property's own documentation — fixed to match the named branch. The property has been write-only in the generator since the feat: Allow non-nullable id field whendefaultModelis set #3437 refactors, so this had no observable effect before; the two test expectations that snapshotted the old value are updated._buildAttachRowImplementationBlockExplicitchecks the enclosing model's id via its first slot when called for the foreign-key-origin side, but the referenced model's id via that same slot when called for a list relation)..any((e) => e.id == null)list guards are gated by the same rule. The relation-field null check in the foreign-side detachRow (if ($field == null)) is kept unconditionally, since object-relation fields are always required to be declared nullable by validation.Removing a guard is behavior-preserving: a statically non-nullable id can never hold null at runtime (the generated constructor coalesces a default, and
fromJson/copyWith/row mapping all funnel through it), and the database layer independently rejects null ids inupdate. The existing integration tests already encode this — for unsaved rows of non-nullable-id models they expectDatabaseUpdateRowException, never theArgumentErrorfrom the guard.All generated code across the repo is regenerated in a separate commit (143 files, deletions only): every relation-bearing file loses the ignore header, and files referencing non-nullable-id models also lose the dead guard blocks. Mixed cases keep exactly the live side, e.g.
CustomerIntAttachRepository.orderskeeps thecustomerInt.idguard (nullableint?id) while theorderUuid.idguard (non-nullable UUID id) is no longer generated — this is the exact example from the issue. Re-runningutil/generate_allon the result produces no diff.New tests in
database_repository_attach_detach_guards_test.dartassert guard presence/absence on the generated method bodies for object relations (origin and foreign side), named and implicit list relations, and mixed-nullability combinations.Fixes #3462
Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.Breaking changes
None. Generated code only loses statically unreachable guard blocks and the now-unnecessary lint suppressions; runtime behavior is unchanged. (
ObjectRelationDefinitiongains a required constructor parameter, but it is internal to the analyzer and constructed only by the resolver and CLI test utilities.)