当我尝试使用 asyncpg 的 executemany 方法时出现神秘的 KeyError

I get a cryptic KeyError when I tryto use asyncpg's executemany method

我正在尝试执行这段代码:

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

其中查询是:

INSERT INTO elexeon_time_series (subject, flow, timestamp, value, update_time, environment) VALUES (, , , , , );

记录由以下数据组成:

[{'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

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

中发送名称参数有问题

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

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