Extra version-only UPDATE per child when saving a new master with a MappedBy ManyValuedAssociation

When a new master with a ManyValuedAssociation(..., 'FMaster') (MappedBy, cascade SaveUpdate) is saved, Aurelius issues an additional UPDATE per child that only bumps the version column, although the FK is already written by the child’s INSERT.

Cause: CascadeSaveUpdate creates the TMasterObjectInsertNotifier only if MappedBy = ''. So the collection’s old state stays empty, UpdateCollections → MergeList treats all children as added, and IncludeInCollection → ExecuteItemAddCommand runs an update with empty ChangedColumns (only VERSION is added).

Effect with 200 children (CachedUpdates, versioned entities): CachedCount 622 instead of 220, flush 537 ms instead of 16 ms.

Suggested fix: create the notifier for MappedBy collections too, or skip ExecuteItemAddCommand when MappedBy <> ''.

We tried it putting this in our override of ExecuteItemAddCommand:

  if (MasterObj.MasterAssociation is TAssociation) and (TAssociation(MasterObj.MasterAssociation).MappedBy <> '') then
    Exit(nil);
  Result := inherited;

Is this workaround safe, or could skipping ExecuteItemAddCommand for MappedBy associations cause other problems (e.g. version handling or OnCollectionItemAdded)?

Hi Felix,

Thank you for the detailed analysis, it made this easy to track down. We have improved this for the next Aurelius release.

One correction first: the extra UPDATE is not version-only. For a MappedBy collection it also writes the foreign key, and it is not always redundant. Aurelius needs it when a child is added to the list but its back-reference is not set:

Child := TChild.Create;      // Child.Master is not set
Master.Children.Add(Child);
Manager.Save(Master);        // the FK is written by that UPDATE, not by the INSERT

So your workaround is not safe in general. Both of your suggested fixes have the same problem. If any code adds a child to a MappedBy list without setting Child.Master, that child's foreign key is never written. A new child ends up with a NULL foreign key, and a child moved from another master keeps pointing to the old master. No error is raised in either case. Skipping the UPDATE also stops OnCollectionItemAdded from firing, which matters if your code or a component such as TMS Echo relies on it. Until you update, the workaround is only safe if every child you add to a MappedBy list also has its back-reference set to that master.

What we changed: Aurelius now skips the UPDATE when both of these are true:

  • the child is inserted in the same save as the master;
  • the child's MappedBy member already points to that master.

In that case the INSERT has already written the foreign key. No UPDATE is issued and the child's version is not incremented. This is how unidirectional associations already behave. The fix applies both to saving a new master and to flushing new children added to a master you already loaded. Children added only to the list, without the back-reference, still get the UPDATE as before.

OnCollectionItemAdded no longer fires for those children, which is also how unidirectional associations behave. OnInserted still fires for each of them.

Once you update, you can remove your ExecuteItemAddCommand override.