【问题标题】:python asyncpg TypeError: _execute() got an unexpected keyword argument 'record_class'python asyncpg TypeError:_execute()得到了一个意外的关键字参数'record_class'
【发布时间】:2021-06-17 03:51:16
【问题描述】:

所以我正在使用 aiohttp 和 asyncpg 开发 REST API。这是我对处理程序的基本看法:

from aiohttp.web_urldispatcher import View
from asyncpgsa import PG


class BaseView(View):
    URL_PATH: str

    @property
    def pg(self) -> PG:
        return self.request.app['pg']

我正在尝试对我的一张表进行选择查询并获取行:

query = select([regions_table.c.region_id]).select_from(regions_table)
regions = await self.pg.fetch(query)

但是,我从标题中得到错误:

File "blahblahblah/env/lib/python3.8/site-packages/asyncpg/connection.py", line 583, in fetch
    return await self._execute(
TypeError: _execute() got an unexpected keyword argument 'record_class'

我的猜测是,当调用没有参数的 execute() 时,fetch 和 fetchrow 有一个参数“record_class”。这是 fetch() 实现:

    async def fetch(
        self,
        query,
        *args,
        timeout=None,
        record_class=None
    ) -> list:
        
        self._check_open()
        return await self._execute(
            query,
            args,
            0,
            timeout,
            record_class=record_class,
        )

这里是_execute():

    def _execute(self, query, args, limit, timeout, return_status=False):
        query, compiled_args = compile_query(query, dialect=self._dialect)
        args = compiled_args or args
        return super()._execute(query, args, limit, timeout,
                                return_status=return_status)

但是我没有看到任何相关的问题,并且来自另一个项目的代码在相同的查询中运行良好。也许我错过了有关文档或处理这些库的内容?欢迎任何建议。

【问题讨论】:

  • 你的包版本似乎有问题,因为asyncpg _execute 的实际来源得到了record_class 参数,你可以在这里查看:github.com/MagicStack/asyncpg/blob/… 你能列出版本你的点子包? pip list
  • @vadimb 我找到了解决方案,谢谢你的线索。

标签: python postgresql rest aiohttp asyncpg


【解决方案1】:

问题是由于asyncpg 与其包装器asyncpgsa 之间的不兼容造成的。我上面粘贴的 fetch() sn-p 来自 asyncpg v0.22 的 asyncpg/connection.py,而 _execute() sn-p 来自 asyncpgsa v0.16.5 的 asyncpgsa/connection.py,现在甚至不是有效的版本。 0.17.0 版本兼容 0.22 asyncpg 及其 record_class 字段,0.16.5 显然已经过时。 所以,我要做的就是重新配置我的 requirements.txt:

asyncpgsa==0.27.0
setuptools~=54.1.2

我相信任何从 skratch 制作 venv 的人都不会遇到同样的问题。我使用了半年前做的项目的需求,所以出现了不兼容的情况。这里的士气是:不要相信copypasta。

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2016-09-17
    • 2019-01-13
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多