dissociate method

Future<void> dissociate({
  1. TChild? related,
})

Clears the declared foreign key. Does not delete any row.

related is required for a to-many: nulling every child's key would be a silent mass write. A non-nullable key column is refused rather than written as NULL.

Implementation

Future<void> dissociate({TChild? related}) async {
  switch (relation.kind) {
    case MssqlRelationKind.belongsTo:
    case MssqlRelationKind.morphTo:
      await _updateParentKeys(_nullParentKeys());
      return;
    case MssqlRelationKind.hasOne:
    case MssqlRelationKind.morphOne:
      if (related != null) {
        await _updateChildKeys(related, _nullChildKeys());
        return;
      }
      await _nullMatchingChildren();
      return;
    case MssqlRelationKind.hasMany:
    case MssqlRelationKind.morphMany:
      if (related == null) {
        throw StateError(
          'dissociate() on to-many "${relation.name}" needs the child '
          'row whose foreign key to clear. Passing none would null every '
          'matching child. delete() is a different decision and is not '
          'implied here.',
        );
      }
      await _updateChildKeys(related, _nullChildKeys());
      return;
    case MssqlRelationKind.belongsToMany:
    case MssqlRelationKind.morphToMany:
      throw StateError(
        'dissociate() on "${relation.name}" is a pivot relation. Use '
        'detach() to remove the link row without touching the target.',
      );
    case MssqlRelationKind.hasOneThrough:
    case MssqlRelationKind.hasManyThrough:
      throw StateError(
        'dissociate() on "${relation.name}" is ${relation.kind.name}: '
        'there is no owned foreign key to clear.',
      );
  }
}