【问题标题】:I get a cryptic KeyError when I tryto use asyncpg's executemany method当我尝试使用 asyncpg 的 executemany 方法时,我得到一个神秘的 KeyError
【发布时间】:2021-10-14 22:38:17
【问题描述】:

我正在尝试执行此代码:

result = await conn.executemany(command=query, args=records)

查询在哪里:

INSERT INTO elexeon_time_series (subject, flow, timestamp, value, update_time, environment) VALUES ($1, $2, $3, $4, $5, $6);

记录由以下数据组成:

[{'environment': 'test',
  'flow': 'test_flow',
  'subject': 'test_subject_K4oESG2YRrnUhld',
  'timestamp': Timestamp('2021-08-11 10:34:19.458810'),
  'update_time': Timestamp('2021-08-11 12:34:19.458810'),
  'value': 0.4},
 {'environment': 'test',
  'flow': 'test_flow',
  'subject': 'test_subject_K4oESG2YRrnUhld',
  'timestamp': Timestamp('2021-08-11 11:34:19.458810'),
  'update_time': Timestamp('2021-08-11 12:34:19.458810'),
  'value': 0.5}]

当我尝试执行这段代码时,我得到了一个相当神秘的回溯。任何人都可以解释这意味着什么?我做错了什么?

test_postgres_aio.py:60: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\eunrg_utils\database\postgres_async.py:33: in async_insert_postgres_data
    result = await conn.executemany(command=query, args=records)
C:\installs\anaconda\envs\eunrg_utils\lib\site-packages\asyncpg\connection.py:355: in executemany
    return await self._executemany(command, args, timeout)
C:\installs\anaconda\envs\eunrg_utils\lib\site-packages\asyncpg\connection.py:1677: in _executemany
    result, _ = await self._do_execute(query, executor, timeout)
C:\installs\anaconda\envs\eunrg_utils\lib\site-packages\asyncpg\connection.py:1711: in _do_execute
    result = await executor(stmt, None)
asyncpg\protocol\protocol.pyx:254: in bind_execute_many
    ???
asyncpg\protocol\coreproto.pyx:945: in asyncpg.protocol.protocol.CoreProtocol._bind_execute_many_more
    ???
asyncpg\protocol\protocol.pyx:220: in genexpr
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

>   ???
E   KeyError: 0

asyncpg\protocol\prepared_stmt.pyx:149: KeyError

【问题讨论】:

    标签: python python-asyncio asyncpg


    【解决方案1】:

    您正在尝试发送字典(此类命名参数)进行查询。据我所知,asyncpg中发送name参数有问题:

    https://github.com/MagicStack/asyncpg/issues/9

    我个人决定不使用asyncpg,并开始使用aiopg,它有更漂亮的(从我的角度来看)API。

    【讨论】:

    猜你喜欢
    • 2012-11-28
    • 1970-01-01
    • 2011-02-09
    • 2015-04-29
    • 2020-09-28
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2016-05-20
    相关资源
    最近更新 更多