-
-
Notifications
You must be signed in to change notification settings - Fork 389
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
KeyError Issue #1665
Comments
和这里的TODO 有关系嘛,是不是一个已知的问题 @jairhenrique @kianmeng @isaquealves @wolph |
These errors doesn't tell anything, apart from that you may be have different scheme at db and at model description If you think there is bug - please provide reproducible self-sufficient example |
Well, in your logs, it shows that there is My guess would be that you can check unicodes of symbols in field name in code and in DB, may be there are symbols that looks identical, but in reality they are different unicode symbols If it doesn't work - I still recommend to debug it around why it throws KeyError in kwargs, as it seems to be real issue here and everything else is just consequences of it |
好的 我会排查的 但排查的同时 我还是保留tortoise-orm 存在问题的观点,因为你还没有解释我上面的问题,keyerror为何会出现1 甚至于 出现了 表名称的字段,这看起来完全不像是 unicode 或编码问题。 并且,我理解 只要确定models中的表设计和数据库中实际的结构是一致的,那么理论上就不应该在这个init的函数中爆出错误了;也就是说只要我确定models是正确的,如果还是init函数中报错,就应该是tortoise-orm的问题了。如果达成这个共识后,我们排查的方向就更加明确了。 另外,想确定下 上一条评论中我的理解是正确的吗。 |
Well, it is indeed strange that it gives error about So only option I see, as it is reproducible on your machine, is to run it with debugger and see concrete values and narrow it down to single values that are failing If debugging is not option in this environment - you can add more logging, that will log all needed values moment before |
好的 明天在工作的时间我会进行调试的。有任何新的进展我会同步在这里,如果你想到了任何 关于 Key 的异常情况(例如:Table、1、表字段),也请及时告知我,以便于及时修复线上问题 |
You should print the value of |
thanks to your reply, i will append it in our code. but in my opinion, its not the essential error. during this code, the param args was just checked by pydantic and passed to this func |
Your code can be refactor to be: @classmethod
async def get_tenant_admin_machine_info(cls, current_user_info, args):
tenant_id = current_user_info.get("tenantId")
tenant_name = current_user_info.get("tenantName")
query_kwargs = dict(tenant_id=tenant_id, **{i.get('fieldName'): i.get('fieldValue') for i in args.fieldQueryInfoList})
try:
tenant_machine_obj = await TenantMachineModel.filter(**query_kwargs)
except Exception as e:
raise custom_error(QuotaErrorCode.QUERY_MACHINE_USER_INFO.dict) from e |
我们似乎也在线上出现了这个bug。 运行一段时间后报错KeyError。 报错的key和当前Model没关系,看起来来自于其它Model @Creat-qc 你们后来是如何解决的? |
Describe the bug
2024-06-21 00:36:31,185 [ERROR] Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 730, in _init_from_db
setattr(self, model_field, kwargs[key])
KeyError: 'tenant_id'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/sanic/resource_quota/components/quota/handler.py", line 1243, in get_tenant_admin_machine_info
tenant_machine_obj = await TenantMachineModel.filter(**query_kwargs).all()
File "/usr/local/lib/python3.9/site-packages/tortoise/queryset.py", line 1006, in _execute
instance_list = await self._db.executor_class(
File "/usr/local/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 138, in execute_select
instance: "Model" = self.model._init_from_db(
File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 747, in _init_from_db
setattr(self, key, meta.fields_map[key].to_python_value(value))
KeyError: '1'
我们的多个项目都使用了tortoise-orm,但是仅有一个项目在一段时间运行后,就会大量的爆出这个错误,但是重启服务之后,这个问题就会被修复,我们比对了多个服务之间使用orm的差异,发现几乎一致,因此想问下这个错误出现的场景和原因应该是怎样的
The text was updated successfully, but these errors were encountered: