2601: Fix creation of deep structures using import in update mode r=mergify[bot] a=ghostwheel42

## What type of PR?

bug-fix

## What does this PR do?

Fixes creation of deep structures (ie user with fetch) when using config-import in update mode.

### Related issue(s)

- closes #2493

## Prerequisites

Before we can consider review and merge, please make sure the following list is done and checked.
If an entry in not applicable, you can check it or remove it from the list.

- [ ] In case of feature or enhancement: documentation updated accordingly
- [ ] Unless it's docs or a minor change: add [changelog](https://mailu.io/master/contributors/workflow.html#changelog) entry file.


Co-authored-by: Alexander Graf <ghostwheel42@users.noreply.github.com>
main
bors[bot] 2 years ago committed by GitHub
commit c729954b4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -909,16 +909,23 @@ class BaseSchema(ma.SQLAlchemyAutoSchema, Storage):
# stabilize import of auto-increment primary keys (not required), # stabilize import of auto-increment primary keys (not required),
# by matching import data to existing items and setting primary key # by matching import data to existing items and setting primary key
if not self._primary in data: if not self._primary in data:
for item in getattr(self.recall('parent'), self.recall('field', 'parent')): parent = self.recall('parent')
existing = self.dump(item, many=False) if parent is not None:
this = existing.pop(self._primary) for item in getattr(parent, self.recall('field', 'parent')):
if data == existing: existing = self.dump(item, many=False)
instance = item this = existing.pop(self._primary)
data[self._primary] = this if data == existing:
break instance = item
data[self._primary] = this
break
# try to load instance # try to load instance
instance = self.instance or self.get_instance(data) instance = self.instance or self.get_instance(data)
# remember instance as parent for pruning siblings
if not self.Meta.sibling and self.context.get('update'):
self.store('parent', instance)
if instance is None: if instance is None:
if '__delete__' in data: if '__delete__' in data:
@ -931,9 +938,6 @@ class BaseSchema(ma.SQLAlchemyAutoSchema, Storage):
else: else:
if self.context.get('update'): if self.context.get('update'):
# remember instance as parent for pruning siblings
if not self.Meta.sibling:
self.store('parent', instance)
# delete instance from session when marked # delete instance from session when marked
if '__delete__' in data: if '__delete__' in data:
self.opts.sqla_session.delete(instance) self.opts.sqla_session.delete(instance)
@ -1014,14 +1018,16 @@ class BaseSchema(ma.SQLAlchemyAutoSchema, Storage):
del_items = True del_items = True
if add_items or del_items: if add_items or del_items:
existing = {item[self._primary] for item in items if self._primary in item} parent = self.recall('parent')
for item in getattr(self.recall('parent'), self.recall('field', 'parent')): if parent is not None:
key = getattr(item, self._primary) existing = {item[self._primary] for item in items if self._primary in item}
if key not in existing: for item in getattr(parent, self.recall('field', 'parent')):
if add_items: key = getattr(item, self._primary)
items.append({self._primary: key}) if key not in existing:
else: if add_items:
items.append({self._primary: key, '__delete__': '?'}) items.append({self._primary: key})
else:
items.append({self._primary: key, '__delete__': '?'})
return items return items

@ -0,0 +1 @@
Fix creation of deep structures using import in update mode
Loading…
Cancel
Save