How do I select literal values in an sqlalchemy query?

You’ll need to use a literal_column, which looks a bit like this:

sqlalchemy.orm.Query(Item, sqlalchemy.sql.expression.literal_column("0"))

Beware that the text argument is inserted into the query without any transformation; this may expose you to a SQL Injection vulnerability if you accept values for the text parameter from outside your application. If that’s something you need, you’ll want to use bindparam, which is about as easy to use; but you will have to invent a name:

sqlalchemy.orm.Query(Item, sqlalchemy.sql.expression.bindparam("zero", 0))

Leave a Comment