Skip to content

refactor: Make checks of id == null on repository classes only present if id is nullable #3462

Open

Description

@marcelomendoncasoares

While implementing feature #3437, attach and detach repository classes were left with unnecessary_null_comparison operations. The checks are generated at repository_classes.dart by the call of _buildCodeBlockThrowIfIdIsNull private method considering that id columns will always be nullable. The calls to such method doesn't check whether the comparison is valid, ending up in code such as:

class CustomerIntAttachRowRepository {
  const CustomerIntAttachRowRepository._();

  /// Creates a relation between this [CustomerInt] and the given [OrderUuid]
  /// by setting the [OrderUuid]'s foreign key `customerId` to refer to this [CustomerInt].
  Future<void> orders(
    _i1.Session session,
    CustomerInt customerInt,
    _i2.OrderUuid orderUuid, {
    _i1.Transaction? transaction,
  }) async {
    if (orderUuid.id == null) {  // <-- unnecessary_null_comparison
      throw ArgumentError.notNull('orderUuid.id');
    }
    if (customerInt.id == null) {
      throw ArgumentError.notNull('customerInt.id');
    }

    var $orderUuid = orderUuid.copyWith(customerId: customerInt.id);
    await session.db.updateRow<_i2.OrderUuid>(
      $orderUuid,
      columns: [_i2.OrderUuid.t.customerId],
      transaction: transaction,
    );
  }
}

The workaround was to add an ignore to this rule when the condition occurs, but the ideal is that the conditions doesn't get generated. This can be done by looking first at the id type of the model inside each _build*ImplementationBlock* method of the repository_classes.dart.

When closing this issue, the hasRelationWithNonNullableIds workaround on model_library_generator.dart must be removed.

Activity

  1. added
    area: models
    on Apr 24, 2025
  2. realmeylisdev commented on Jun 12, 2026

    @realmeylisdev
    Contributor

    @marcelomendoncasoares short summary of the investigation behind the fix (#5318):

    • Root cause confirmed: _buildCodeBlockThrowIfIdIsNull/...AnyIdIsNull are string-based and consult no type info; ~15 call sites emit unconditionally. Gating must be per checked object — one method can guard two ids with different nullability (e.g. CustomerInt + OrderUuid).
    • The blocker: the referenced model's id nullability was unavailable to the generator for FK-origin object relations — parentTableIdType stores the FK owner's id, and module models (e.g. module:auth:UserInfo) never reach the generator pipeline. Fixed at resolution time with a new ObjectRelationDefinition.referenceIdType.
    • Bonus latent bug: the implicit list-relation branch stored the declaring class's id in foreignKeyOwnerIdType instead of the FK owner's (contradicting its doc). Unobservable until now — both properties have been write-only in the generator since feat: Allow non-nullable id field when defaultModel is set #3437.
    • Removal is behavior-preserving: non-nullable ids (UUID + defaultModel, incl. inherited ids) are constructor-coalesced and can never be null; the DB layer independently rejects null ids; integration tests already expect DatabaseUpdateRowException for unsaved non-nullable-id rows.
    • The hasRelationWithNonNullableIds workaround (which never actually inspected id nullability — it suppressed the lints for every relation-bearing model) is removed, along with the dead_code ignore added by the Dart 3.10 bump.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: models

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions