sqlitepeewee

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


I have a SQLite-Select statement like this to get the next available number: SELECT IFNULL(MAX(current_nr)+1, 1) FROM rm_evaluation;

I already have a corresponding model in python peewee: class RmRiskEvaluation(BaseModel): ... current_nr = IntegerField(unique=True) class Meta: table_name = 'rm_evaluation'

But how can I express the SQL-statement from above. => Get the last number, add 1 to it and return the whole thang; if there is no last number at all, calculate with 1 beforehand.


Solution

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