fix: check attr exists before setattr to aviod KeyError #1416
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
After the service has been running for a period of time, the tortoise orm will suddenly report an error and need to restart the service to repair it.
Additional context
Add any other context about the problem here.
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/tortoise/models.py", line 717, in _init_from_db
setattr(self, model_field, kwargs[key])
KeyError: 'images'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/frontend/api.py", line 3296, in viewRecord
scheme_obj = await Scheme.get_or_none(uni_id=scheme_id)
File "/usr/local/lib/python3.8/dist-packages/tortoise/queryset.py", line 908, in _execute
instance_list = await self._db.executor_class(
File "/usr/local/lib/python3.8/dist-packages/tortoise/backends/base/executor.py", line 134, in execute_select
instance: "Model" = self.model._init_from_db(
File "/usr/local/lib/python3.8/dist-packages/tortoise/models.py", line 734, in _init_from_db
setattr(self, key, meta.fields_map[key].to_python_value(value))
KeyError: 'watch_user_id'
Traceback (most recent call last):
File "/data/frontend/api.py", line 1446, in schemeList
s['sale_num'] = await Order.filter(scheme_id=s['uni_id'], status=1).count()
File "/usr/local/lib/python3.8/dist-packages/tortoise/queryset.py", line 1174, in _execute
count = list(dict(result[0]).values())[0] - self.offset
IndexError: list index out of range
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/tortoise/models.py", line 717, in _init_from_db
setattr(self, model_field, kwargs[key])
KeyError: 'nickname'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/frontend/api.py", line 474, in getSubscribe
wx_user_obj = await WXUserInfo.get_or_none(user_id=request.ctx.uid, app_id=ACTIVE_WX_APP_ID)
File "/usr/local/lib/python3.8/dist-packages/tortoise/queryset.py", line 908, in _execute
instance_list = await self._db.executor_class(
File "/usr/local/lib/python3.8/dist-packages/tortoise/backends/base/executor.py", line 134, in execute_select
instance: "Model" = self.model._init_from_db(
File "/usr/local/lib/python3.8/dist-packages/tortoise/models.py", line 734, in _init_from_db
setattr(self, key, meta.fields_map[key].to_python_value(value))
KeyError: 'COUNT(*)'
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/tortoise/models.py", line 717, in _init_from_db
setattr(self, model_field, kwargs[key])
KeyError: 'user_id'
Motivation and Context
Check the attr exists before setting the attr to avoid the KeyError
from the error stack we can see there's some key name "Count(*)". it should not be the attr for a model
How Has This Been Tested?
Checklist: