Model API gives wrong value

The model API is not working as I would expect.

Upon running

val1 = Pool().get('res.user')(Transaction().user).employee
val2 = Pool().get('res.user').search([
    ('id', '=', Transaction().user)])[0].employee

val1 is unexpectedly None, while val2 is the correct employee instance. If I reverse the line order, then val1 and val2 both have the correct value.

This is happening within set_user and set_context context managers, and the problem only happens for one Employee. Moving that Employee from one User to another alters which User experiences the discrepancy, yet I cannot find anything unusual about that Employee when comparing all of its fields with another Employee who does not trigger the problem.

When reading all the fields on the User generated the val1 way and comparing them to the fields on the User generated the val2 way all the fields are the same, with the exception of employee and password_hash, which are None on the val1 version.

Can anyone give me insight into what is happening here? Are there any idioms I
should follow to avoid this surprise?

This is happening on trytond 5.0.38.

On 5.0 series, the record rules should apply on such code.
So I would suspect that the .search prefill the cache and thus skip the rule.

It will be interesting to to see if this is happening on latest versions.

Thank you for the fast and helpful response.

I was not thinking through the full consequences of switching users using the set_user context manager. I have adopted a different solution to avoid doing that, and now it is working as expected.

(I know this is a not a full explanation of what was happening with the cache.)

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.