sqlalchemy cant bind order by clause

我是研究僧i 提交于 2020-07-31 05:25:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!