asyncpg DataError: invalid input for query argument expected str, got int

231 views Asked by At

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?

1

There are 1 answers

0
Dorian Turba On BEST ANSWER

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.