SQLAlchemy does not autoconvert type when using asyncpg
#6365
Replies: 2 comments 10 replies
-
SQLAlchemy doesn't infer types when they are passed to connection.execute(), it only does its own inference when SQL constructs are being constructed, such as if you say something like if asyncpg is complaining about types you would have to supply these upfront. you can send the values directly to text.bindparams() where it will infer the types: stmt = text(
'INSERT INTO dtype_test (index, "A", "B") VALUES(:index, :a, :b)'
).bindparams(index=0, a=0.8, b=True) you could use those values as placeholders, they will be overridden if you supply different values to .execute(). or you can send just the datatypes, but the syntax is more verbose at the moment: stmt = text(
'INSERT INTO dtype_test (index, "A", "B") VALUES(:index, :a, :b)'
).bindparams(bindparam("index", type_=Integer), bindparam("a", type_=Float), bindparam("b", type_=Boolean))) |
Beta Was this translation helpful? Give feedback.
-
I think the difference in behaviour is actually on the dbapi level. You can verify this by adding
As you can see |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
Whenever I use SQLAlchemy with
asyncpg
and I input the incorrect type SQLAlchemy does not attempt to convert the type to the correct type, unlike when I am using SQLAlchemy withpsycopg2
. See the examples below for reference. I am not sure if this intended behavior or not.To Reproduce
Using
psycopg2
Using
asyncpg
Error
Versions.
asyncpg
andpsycopg2
Have a nice day!
Beta Was this translation helpful? Give feedback.
All reactions