Giter Club home page Giter Club logo

Comments (10)

waketzheng avatar waketzheng commented on August 24, 2024 1

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

from tortoise-orm.

Creat-qc avatar Creat-qc commented on August 24, 2024
    except KeyError:
        self._partial = True
        # TODO: Apply similar perf optimisation as above for partial
        for key, value in kwargs.items():
            setattr(self, key, meta.fields_map[key].to_python_value(value))

和这里的TODO 有关系嘛,是不是一个已知的问题 @jairhenrique @kianmeng @isaquealves @wolph

from tortoise-orm.

abondar avatar abondar commented on August 24, 2024

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

from tortoise-orm.

Creat-qc avatar Creat-qc commented on August 24, 2024

好的,我在你们的_init_from_db函数中加上了如下简单的代码注释
image
image

下图是日志的打印结果:
image

我们的models
image

image

我们的实际数据库表
image

可以看出实际数据库与models中的定义是一致的

且通过打印日志发现,正常情况下 map是正确的,但是在异常情况下,会引发不可恢复的致命问题

这是我截取的相关日志
image

下面是我对tortoise-orm 的相关实现的理解

通过修改源码,打印日志发现,应该是 需要特殊处理的参数 在做映射时出现了 找不到key 从而找不到 本应映射到的 参数类型,进而引发了 keyError问题;tortoise-orm 在处理model时,会做一层缓存机制,也就是将model中的key 应该映射成什么类型进行缓存。所以引发KeyError的本质原因有可能是 缓存中的map 与实际不一致,因此报错,并且在服务重启后,缓存清空,所以重启服务后可以正常运行一段时间。

为什么 会出现 COUNT(*)、数字 1 作为 Key 的情况

@jairhenrique @kianmeng @isaquealves @wolph @abondar

from tortoise-orm.

abondar avatar abondar commented on August 24, 2024

Well, in your logs, it shows that there is tenant_id present in kwargs, and it is present, so I don't know why error occurs, but at this point it doesn't looks like something tortoise responsible for

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

from tortoise-orm.

Creat-qc avatar Creat-qc commented on August 24, 2024

好的 我会排查的 但排查的同时 我还是保留tortoise-orm 存在问题的观点,因为你还没有解释我上面的问题,keyerror为何会出现1 甚至于 出现了 表名称的字段,这看起来完全不像是 unicode 或编码问题。

并且,我理解 只要确定models中的表设计和数据库中实际的结构是一致的,那么理论上就不应该在这个init的函数中爆出错误了;也就是说只要我确定models是正确的,如果还是init函数中报错,就应该是tortoise-orm的问题了。如果达成这个共识后,我们排查的方向就更加明确了。

另外,想确定下 上一条评论中我的理解是正确的吗。

@abondar @grigi @zoliszeredi @AEnterprise @reedjosh

from tortoise-orm.

abondar avatar abondar commented on August 24, 2024

Well, it is indeed strange that it gives error about KeyError: 1, which I don't see how is happening with values that are seen in logged values
But there isn't really much I can do without reproducible example, that I would be able to run locally

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 setattr operation is happening and see state of all variable at that exact moment

from tortoise-orm.

Creat-qc avatar Creat-qc commented on August 24, 2024

好的 明天在工作的时间我会进行调试的。有任何新的进展我会同步在这里,如果你想到了任何 关于 Key 的异常情况(例如:Table、1、表字段),也请及时告知我,以便于及时修复线上问题

from tortoise-orm.

waketzheng avatar waketzheng commented on August 24, 2024

You should print the value of query_kwargs before line 1243:
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()

from tortoise-orm.

Creat-qc avatar Creat-qc commented on August 24, 2024
@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()
        query_kwargs["tenant_id"] = tenant_id

        for field_query_info_item in args.fieldQueryInfoList:
            query_kwargs[field_query_info_item.get("fieldName")] = field_query_info_item.get("fieldValue")

        try:
            tenant_machine_obj = await TenantMachineModel.filter(**query_kwargs).all()
        except Exception as e:
            raise custom_error(QuotaErrorCode.QUERY_MACHINE_USER_INFO.dict) from e

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

from tortoise-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.