Skip to content

Behavior of Skeleton.patch() when given key was not found #1565

@phorward

Description

@phorward

With viur-core 3.8, an update_relations-Tasks fails at this call:

if not skel.patch(lambda skel: skel.refresh(), key=src_rel["src"].key, update_relations=False):
    logging.warning(f"Cannot update stale reference to {src_rel["src"].key!r} referenced by {src_rel.key!r}")

With this error:

Creation during update is forbidden - explicitly provide `create=True` to allow.
Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/viur/core/tasks.py", line 246, in deferred
    _deferred_tasks[funcPath](*args, **kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/viur/core/skeleton/tasks.py", line 90, in update_relations
    if not skel.patch(lambda skel: skel.refresh(), key=src_rel["src"].key, update_relations=False):
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/viur/core/skeleton/skeleton.py", line 806, in patch
    return db.run_in_transaction(__update_txn)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/viur/core/db/transport.py", line 123, in run_in_transaction
    res = func(*args, **kwargs)
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/viur/core/skeleton/skeleton.py", line 755, in __update_txn
    raise ValueError("Creation during update is forbidden - explicitly provide `create=True` to allow.")
ValueError: Creation during update is forbidden - explicitly provide `create=True` to allow.

Solution for this problem would be

try:
    skel.patch(lambda skel: skel.refresh(), key=src_rel["src"].key, update_relations=False)
except ValueError:
    logging.warning(f"Cannot update stale reference to {src_rel["src"].key!r} referenced by {src_rel.key!r}")

But IMHO, the original handling of the function shown above is more likely to Skeleton.read(), with create=False.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions