-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intermittent RelatedObjectDoesNotExist
Error in Computed Fields
#156
Comments
At a first glance - yes this looks like an issue with the mro. Can you re-check, whether for a successful run the mro looks the same? To further debug things - can you show, what the admin gives for that model under "Computedfields" >> "Computed Fields Models" >> your_model including its ModelGraph? (You can switch on the admin helper views in your settings.py by defining |
In a successful run, the MRO is different — the currency_id appears before price_total, preventing the issue from occurring. I noticed that the MRO displayed in the admin panel changes every time I restart the server. This seems odd to me because, as far as I understand, the MRO should only change if there's a modification to the database model or the class hierarchy. Could you confirm if my understanding is correct, or is there another reason why the MRO would behave this way? |
Eww thats a serious bug - yepp, the mro must be stable for a certain dependency configuration, it may never flip positions. |
Yes, this is a critical bug, and I hope it can be resolved as soon as possible since I have a project similar to Odoo that relies on it. I will also continue to investigate the issue further, and if I discover anything useful, I will share it with you promptly. |
We got a sentry alert about RelatedFieldDoesNotExist today, where it certainly exists. Tested restarting and indeed the MRO changed in the admin. Before restart:
After restart:
Seems like potentially enforcing alphabetical order could be a quick fix? |
@wadewilliams Sorry I had no time yet to look at the issue and where the mixup happens. In theory this should not have happened, because of how it is coded:
By that there is no possibility to mix things up (thats at least what I thought lol). My guess currently goes to the way how django creates fk fields on models - could it be, that they are not stable in init time? Edit: Alphabetical ordering might be a quickfix yes, but it would have to be done on the topological groups, not model globally. |
I shipped this patch for the time being that at least suppresses the error: from functools import wraps
import computedfields
from django.core.exceptions import ObjectDoesNotExist
def computedfield_factory(field, compute, **kwargs):
@wraps(compute)
def func(instance):
# Suppress intermittent RelatedObjectDoesNotExist error
# https://github.com/netzkolchose/django-computedfields/issues/156
try:
return compute(instance)
except ObjectDoesNotExist:
return field.get_default()
return computedfields.models.ComputedField(field, func, **kwargs)
ComputedField = computedfield_factory Note values do end up wrong with this but it's better than erroring. |
@stevelacey Thx for the idea, for follow-up I gonna comment on #157 instead. |
Issue Overview
I am encountering an intermittent
RelatedObjectDoesNotExist
error inside one of my computed functions that depends on another computed field. The error occurs sporadically and can be temporarily resolved by restarting the server.Here is an example of the problematic code:
Error Details
The error looks like this:
Here is the definition of the currency computed field that seems to cause the issue:
Observations
This issue only happens sometimes and can be resolved by stopping and restarting the server.
Debugging indicates that the cf_mro (computed fields method resolution order) appears incorrect at times. Below is the relevant portion of the code for debugging:
Key Questions
Expected Behavior
Steps to Reproduce
Additional Notes
The issue seems related to how the computed fields dependencies are resolved (cf_mro).
Any insight into why cf_mro is inconsistent or incorrect would be greatly appreciated.
The text was updated successfully, but these errors were encountered: