In Peewee 3.7.0, when executing the following code I get this error:
TypeError: '>' not supported between instances of 'ModelCompoundSelectQuery' and 'int'
def myMethod(lastItemDbId):
# lastItemDbId is an int
...
columnX = fn.Lower(People.name)
lastItemColumnX = People.select(columnX).where(People.id == lastItemDbId)
results = People.select(People.id).where(People.parent.is_null() & (columnX > lastItemColumnX | (columnX == lastItemColumnX & People.id > lastItemDbId))).order_by(columnX, People.id).limit(limit)
The problem seems to be with the People.id > lastItemDbId
section, but I can't understand why.
Removing the People.id > lastItemDbId
part, the error goes away... but testing that part alone also works (see below)
results = People.select(People.id).where(People.id > lastItemDbId).order_by(columnX, People.id).limit(limit)
In case you are wondering, this is the Peewee model definition:
class People(Model):
name = CharField()
parent = ForeignKeyField('self', backref='children', null=True, on_delete='CASCADE')
From Peewee documentation:
Note that the actual comparisons are wrapped in parentheses. Python’s operator precedence necessitates that comparisons be wrapped in parentheses.
Even if apparently redundant, the solution is to wrap in parenthesis every logical section found to the left and right of a bitwise operators like "&" and "|".
So the solution would be:
results = People.select(People.id).where((People.parent.is_null()) & ((columnX > lastItemColumnX) | ((columnX == lastItemColumnX) & (People.id > lastItemDbId)))).order_by(columnX, People.id).limit(limit)