我被这个难以解释的 asyncpg 错误所困扰。该错误告诉我索引 0 的类型错误,在接收 int 时需要日期时间:
asyncpg.exceptions.DataError: invalid input for query argument $1: [(None, 1000, 2001, datetime.datetime(2.... (invalid array element at index 0: expected a datetime.date or datetime.datetime instance, got 'int')
这很奇怪,因为架构是:
id: bigint,
eid: bigint,
time: datetime,
type: varchar(20)
我的数据如下所示:
data = [(None, 1000, 2001, datetime.datetime(2020, 3, 20, 23, 20, 35, 71), 'Testing1'),
(None, 1001, 2002, datetime.datetime(2020, 3, 20, 23, 20, 35, 71), 'Testing2'),
(None, 1002, 2003, datetime.datetime(2020, 3, 20, 23, 20, 35, 71), 'Testing3')]
没有添加到第一个元素,因为我在另一篇文章中看到了,没有它,同样的错误。
和异步功能:
async def insert(data):
conn = await asyncpg.connect(user='username', password='password',
database='database', host='127.0.0.1')
res = await conn.fetch('''
INSERT INTO tablename (id, eid, time, type)
(SELECT
e.id, e.eid, e.time, e.type
FROM
unnest($1::tablename[]) as e
)
''', data)
await conn.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(insert(data))
非常不清楚的错误,希望anynoe可以帮助解决这个问题。