Skip to content
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

fix: check attr exists before setattr to aviod KeyError #1416

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

ezioruan
Copy link

@ezioruan ezioruan commented Jul 3, 2023

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:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@ggjkb22
Copy link

ggjkb22 commented Jul 19, 2023

After completing this change, the queryset will report a KeyError again. I tried to avoid this error by reducing the version, but versions such as 0.19.3 and 0.18.1 all have this issue

@ezioruan
Copy link
Author

can you share you error stack? @ggjkb22

@ggjkb22
Copy link

ggjkb22 commented Jul 21, 2023

Similar to your error stack, do you no longer report errors after the above modifications?
@ezioruan

@ezioruan
Copy link
Author

@long2ice could you help review?

@long2ice
Copy link
Member

Can you show your code? This change maybe just avoid it but not fix it.

@ezioruan
Copy link
Author

yes. But I don't know how to fix it. do you have any idea @long2ice

smartydroid added a commit to smartydroid/tortoise-orm that referenced this pull request Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants