I admittedly don't understand how asyncpg's codecs work, but it seems to work counter to how I'd expect:
import asyncio
import asyncpg
async def main():
conn = await asyncpg.connect('postgresql://postgres@localhost/test')
print(await conn.fetchval("select $1", 'a')) # this works: prints 'a'
print(await conn.fetchval("select $1", 1)) # invalid input for query argument $1: 1 (expected str, got int)
asyncio.run(main())
It seems asyncpg wants the parameter to always be a string, but I want it to be an int. Why does this fail?
Specify postgresql type
int
Using
conn.fetchval("select $1::int", 1)
might solve your issue.We can see an example of parameter type set to int here.