You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
refactor: Make checks of id == null on repository classes only present if id is nullable #3462
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:
classCustomerIntAttachRowRepository {
constCustomerIntAttachRowRepository._();
/// 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_comparisonthrowArgumentError.notNull('orderUuid.id');
}
if (customerInt.id ==null) {
throwArgumentError.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.
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.
Activity
realmeylisdev commentedon Jun 12, 2026
@marcelomendoncasoares short summary of the investigation behind the fix (#5318):
_buildCodeBlockThrowIfIdIsNull/...AnyIdIsNullare 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).parentTableIdTypestores the FK owner's id, and module models (e.g.module:auth:UserInfo) never reach the generator pipeline. Fixed at resolution time with a newObjectRelationDefinition.referenceIdType.foreignKeyOwnerIdTypeinstead 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 whendefaultModelis set #3437.defaultModel, incl. inherited ids) are constructor-coalesced and can never be null; the DB layer independently rejects null ids; integration tests already expectDatabaseUpdateRowExceptionfor unsaved non-nullable-id rows.hasRelationWithNonNullableIdsworkaround (which never actually inspected id nullability — it suppressed the lints for every relation-bearing model) is removed, along with thedead_codeignore added by the Dart 3.10 bump.