解决 sqlalchemy报Textual SQL expression should be explicitly declared as text的错误

1. 现象

执行某句sql的时候报错

Textual SQL expression 'template_type desc'  should be explicitly declared as text('template_type desc')

2. 原因

检查我们的python包依赖,发现之前使用的 sqlalchmy 版本为 1.2.x,现在使用的版本为1.3.x,问题出现在这里吗?
检查版本更新记录,在sqlalchemy 1.3 的 Key Changes - Core 部分发现 text() 处有改动,原来 sqlalchemy 在 1.3.x 之后 string SQL 由warning 变成了 Coercion,

The warnings that were first added in version 1.0, described at Warnings emitted when coercing full SQL fragments into text(), have now been converted into exceptions

也就是说1.3 之前使用如 order_by("template_type desc")这种方式只是会报警告的信息,但是在1.3之后的这种写法就会 raise CompileError

3. 解决方法

将写法由 order_by("template_type desc") 改为 order_by(text("template_type desc"))

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值