问题
I am trying to bind the order by clause and limit clause in my query with no luck using SQLAlchemy
print(engine.execute(text('''select vendor_id from vendors ORDER BY :column_n :order_n'''), column_n='vendor_id', order_n='DESC').fetchall())
print(engine.execute('''select vendor_id from vendors order by vendor_id DESC''').fetchall())
Query 2 above is producing the expected output 2 below (of course it is not parameterized):
2020-06-10 10:51:51,126 INFO sqlalchemy.engine.base.Engine select vendor_id from vendors ORDER BY %(column_n)s %(order_n)s
2020-06-10 10:51:51,126 INFO sqlalchemy.engine.base.Engine {'column_n': 'vendor_id', 'order_n': 'DESC'}
[(1,), (2,), (555,), (556,)]
2020-06-10 10:51:51,128 INFO sqlalchemy.engine.base.Engine select vendor_id from vendors order by vendor_id DESC
2020-06-10 10:51:51,129 INFO sqlalchemy.engine.base.Engine {}
[(556,), (555,), (2,), (1,)]
Is it not possible to dynamically parameterize the order by
clause?
I am using MySQL 8, Python 3.8
来源:https://stackoverflow.com/questions/62306602/sqlalchemy-cant-bind-order-by-clause