diff --git a/src/protean/core/repository/base.py b/src/protean/core/repository/base.py index c7cfed41..faf5fb78 100644 --- a/src/protean/core/repository/base.py +++ b/src/protean/core/repository/base.py @@ -112,12 +112,6 @@ def add(self, aggregate): # noqa: C901 logger.error(errors) raise ValidationError(errors) - # Persist only if the aggregate object is new, or it has changed since last persistence - if ((not aggregate.state_.is_persisted) or - (aggregate.state_.is_persisted and aggregate.state_.is_changed)): - dao = current_domain.get_dao(self.meta_.aggregate_cls) - dao.save(aggregate) - # If there are HasMany fields in the aggregate, sync child objects added/removed, # but not yet persisted to the database. # @@ -160,6 +154,12 @@ def add(self, aggregate): # noqa: C901 field.change = None field.change_old_value = None + # Persist only if the aggregate object is new, or it has changed since last persistence + if ((not aggregate.state_.is_persisted) or + (aggregate.state_.is_persisted and aggregate.state_.is_changed)): + dao = current_domain.get_dao(self.meta_.aggregate_cls) + dao.save(aggregate) + return aggregate def remove(self, aggregate):