MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.99k stars 404 forks source link

Errors with parameters #975

Closed ponomar closed 1 year ago

ponomar commented 2 years ago

Case 1

await fetch('SELECT $1', 1)

asyncpg.exceptions.DataError: invalid input for query argument $1: 1 (expected str, got int)

While 2 similar work fine:

await fetch('SELECT $1::int', 1)
await fetch('SELECT $1', '1')

It can not handle integers automatically?

Case 2

await fetch('select case when true then $1 end', 1)

Fails with the same error as above, while next query works fine (when parameter in Else statement):

await fetch('select case when true then 1 else $1 end', 1) 

Case 3

await fetch('select val is $1 from unnest(array[1,null]) as x(val)', None)

asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "$1"

ponomar commented 2 years ago

I mean if these are small bugs or these behaviours are designed such way?

elprans commented 2 years ago

This is expected behavior. Asyncpg relies on PostgreSQL type inference to determine the expected types of the arguments. In SELECT $1 there is no context, so the default text type is inferred.