如何在 peewee 中用 max() 和 ifnull() 表达 select

How to express select with max() and ifnull() in peewee

我有一个像这样的 SQLite-Select 语句来获取下一个 available 号码: SELECT IFNULL(MAX(current_nr)+1, 1) FROM rm_evaluation;

我在python peewee中已经有对应的型号了: class RmRiskEvaluation(BaseModel): ... current_nr = IntegerField(unique=True) class Meta: table_name = 'rm_evaluation'

但是我如何表达上面的SQL-语句。 => 获取最后一个数字,将其加 1 并 return 整个数字;如果根本没有最后一个数字,则预先计算为1。

如果您不是那么懒惰,甚至花几分钟阅读文档或搜索,您就会找到答案。

fn.IFNULL(fn.MAX(RmRiskEvaluation.current_nr) + 1, 1)