Closed negcx closed 1 year ago
Unlike psycopg2, asyncpg does uses the binary data I/O and never does parameter substitution on the client side. Server-side arguments are used instead, so you should expect certain differences in behavior.
These should work:
@pytest.mark.asyncio
async def test_asyncpg_integer_cast():
"""
asyncpg.exceptions.DataError: invalid input for query argument $1:
'1' ('str' object cannot be interpreted as an integer)
"""
conn = await asyncpg.connect(DSN)
await conn.execute(TABLE)
await conn.execute(
"select id, date from asyncpg_issue where id = $1::integer",
1 # <- pass an actual Python integer
# if you really need to pass a string, cast $1 to text first:
# $1::text::integer.
)
await conn.close()
@pytest.mark.asyncio
async def test_asyncpg_interval():
"""
asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "$1"
"""
conn = await asyncpg.connect(DSN)
await conn.execute(TABLE)
await conn.execute(
"select id, date from asyncpg_issue where date > now() + $1::text::interval",
'2 weeks ago'
)
conn.close()
Thank you - that makes a lot of sense. These do work and I figured there was something like that. I appreciate your help!
asyncpg
throws aDataError
when I pass a string and try to cast it as an integer within the query.asyncpg
throws aPostgresSyntaxError
when I try to use a parameter with a string and an interval, such asinterval $1
where $1 is'2 weeks ago'
.I have created a repository with tests to reproduce the errors here: https://github.com/negcx/asyncpg-type-issue. This also includes passing tests when calling
psycopg2
. As the code is relatively short, I've also added it here below.Thank you!