Peewee query - TypeError: '>' not supported between instances of 'ModelCompoundSelectQuery' and 'int'
Peewee query - TypeError: '>' not supported between instances of 'ModelCompoundSelectQuery' and 'int'
在 Peewee 3.7.0 中,执行以下代码时出现此错误:
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)
问题似乎出在 People.id > lastItemDbId
部分,但我不明白为什么。
删除 People.id > lastItemDbId
部分,错误消失......但单独测试该部分也有效(见下文)
results = People.select(People.id).where(People.id > lastItemDbId).order_by(columnX, People.id).limit(limit)
如果您想知道,这是 Peewee 模型定义:
class People(Model):
name = CharField()
parent = ForeignKeyField('self', backref='children', null=True, on_delete='CASCADE')
来自 Peewee 文档:
Note that the actual comparisons are wrapped in parentheses. Python’s operator precedence necessitates that comparisons be wrapped in parentheses.
即使明显多余,解决方案是将在“&”和“|”等按位运算符左侧和右侧找到的每个逻辑部分括在括号中。
所以解决方案是:
results = People.select(People.id).where((People.parent.is_null()) & ((columnX > lastItemColumnX) | ((columnX == lastItemColumnX) & (People.id > lastItemDbId)))).order_by(columnX, People.id).limit(limit)
在 Peewee 3.7.0 中,执行以下代码时出现此错误:
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)
问题似乎出在 People.id > lastItemDbId
部分,但我不明白为什么。
删除 People.id > lastItemDbId
部分,错误消失......但单独测试该部分也有效(见下文)
results = People.select(People.id).where(People.id > lastItemDbId).order_by(columnX, People.id).limit(limit)
如果您想知道,这是 Peewee 模型定义:
class People(Model):
name = CharField()
parent = ForeignKeyField('self', backref='children', null=True, on_delete='CASCADE')
来自 Peewee 文档:
Note that the actual comparisons are wrapped in parentheses. Python’s operator precedence necessitates that comparisons be wrapped in parentheses.
即使明显多余,解决方案是将在“&”和“|”等按位运算符左侧和右侧找到的每个逻辑部分括在括号中。
所以解决方案是:
results = People.select(People.id).where((People.parent.is_null()) & ((columnX > lastItemColumnX) | ((columnX == lastItemColumnX) & (People.id > lastItemDbId)))).order_by(columnX, People.id).limit(limit)