associate method

Future<void> associate(
  1. TChild related
)

Points this parent at related, or related at this parent.

belongsTo / morphTo rewrite the parent's key columns. hasOne / hasMany / morph-one/many rewrite the child's. Pivot shapes use attach instead: associating would have to invent a pivot row.

Implementation

Future<void> associate(TChild related) async {
  switch (relation.kind) {
    case MssqlRelationKind.belongsTo:
    case MssqlRelationKind.morphTo:
      await _updateParentKeys(_parentKeyAssignments(related));
      return;
    case MssqlRelationKind.hasOne:
    case MssqlRelationKind.hasMany:
    case MssqlRelationKind.morphOne:
    case MssqlRelationKind.morphMany:
      await _updateChildKeys(related, _childKeyAssignments());
      return;
    case MssqlRelationKind.belongsToMany:
    case MssqlRelationKind.morphToMany:
    case MssqlRelationKind.hasOneThrough:
    case MssqlRelationKind.hasManyThrough:
      throw StateError(
        'associate() on "${relation.name}" is ${relation.kind.name}. '
        'Use attach() for a declared pivot; through relations have no '
        'single key to rewrite.',
      );
  }
}