0

所以我正在使用 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)

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

4

1 回答 1

0

问题是由于asyncpg及其 wrapper之间的不兼容造成的asyncpgsa。我在上面粘贴的 fetch() 片段来自 asyncpg v0.22 的 asyncpg/connection.py,而 _execute() 片段来自 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。

于 2021-03-20T08:37:05.700 回答