sync method

Future<OrmMigrationRecord> sync(
  1. String relationName,
  2. List ids, {
  3. Map<String, dynamic>? pivotData,
})
inherited

Syncs a manyToMany relationship to match the given IDs exactly.

This replaces all existing pivot records with new ones for the given IDs. Internally calls detach() (all) followed by attach().

Example:

final post = await Post.query().find(1);
// Currently has tags: [1, 2, 3]
await post.sync('tags', [2, 3, 4]);
// Now has tags: [2, 3, 4]

Implementation

Future<TModel> sync(
  String relationName,
  List<dynamic> ids, {
  Map<String, dynamic>? pivotData,
}) async {
  // First detach all
  await detach(relationName);

  // Then attach the new IDs
  if (ids.isNotEmpty) {
    await attach(relationName, ids, pivotData: pivotData);
  }

  return _self();
}