I want to execute some raw SQL and take advantage of SQLAlchemy's SQL injection prevention.
My code is similar to this bit:
import sqlalchemy
from sqlalchemy.sql import text
DB_URI = '...'
engine = sqlalchemy.create_engine(DB_URI)
sql = text("SELECT * FROM some_table WHERE userid = :userid")
res = engine.execute(sql, userid=12345)
# do something with the result...
The problem is that userid
in some_table
is of type varchar
. All I want to do is to tell SQLAlchemy to convert 12345
to a string before executing the statement. I know how I could do the conversion both in Python and in SQL. But I remember that I once used a explicit type definition in SQLAlchemy, I just can't find it anymore. Can someone point me to the right direction?
(My actual question involves postgresql arrays of BIGINTs vs. INTs, but I tried to keep it simple.)
Thanks for any help!