Skip to content

[17.0][18.0] auditlog: related+stored fields have no value after a create call when a rule monitors creation events #3410

@MaxyMoos

Description

@MaxyMoos

Module

auditlog

Describe the bug

Related fields with store=True have no value (False) after a create call on models where an auditlog.rule is defined for creation events.
This issue is not reproducible with pure computed fields or with non-stored related fields.

To Reproduce

Affected versions: 17.0, 18.0

Steps to reproduce the behavior:

  1. Create two simple models with a related field like so:
Image
  1. Add an auditlog.rule on the model.two, logging for creations
  2. Create a model.two instance and check the related field value:
Image

Expected behavior
The related field should have the correct value after the create call:

Image

(this screenshot was obtained with server-tools commit 51a4e9c, right before #3373 was merged, which introduced this issue)

Workaround
I've tried some things in the ThrowAwayCache class and this modified __exit__ method below seems to fix the issue described here, but it's really just a starting point and probably has some unintended side effects as i'm not familiar with Odoo cache handling. However, the current auditlog module tests seem to be passing with it:

def __exit__(self, exc_type, exc_val, exc_tb):
    """Restore the original cache wherever it was replaced."""
    temp_cache = self._transaction.cache
    for field, field_cache in temp_cache._data.items():
        if field.related and field.store:
            if field not in self._original_cache._data.keys():
                self._original_cache._data[field] = field_cache
            else:
                self._original_cache._data[field].update(field_cache)

    for env in self._transaction.envs:
        env.cache = self._original_cache
    self._transaction.cache = self._original_cache

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions