首页 > 解决方案 > Peewee 3 - Python - 排名查询结果

问题描述

我有一个带有注释(id、title、parent)的自引用数据库表。
[注意:表格的自引用属性与问题无关]
我想按标题的字母顺序对它们进行排序,并通过id. 我使用 Peewee 3 作为 ORM。

数据库型号:

class Note(Model):
    title = CharField()
    parent = ForeignKeyField('self', backref='children', null = True)

代码:

noteAlias = Note.alias()
subquery = (noteAlias.select(noteAlias.id, fn.RANK().over(partition_by=[noteAlias.title], order_by=[noteAlias.title]).alias('rank')).where(noteAlias.parent.is_null()).alias('subq'))
query = (Note.select(subquery.c.id, subquery.c.rank).from_(subquery).where(subquery.c.id == 5))
print("Rank of element is: " + str(query.rank))

这段代码给了我以下错误:

cursor.execute(sql, params or ())
sqlite3.OperationalError: near "(": syntax error

SQLite 测试
如果我直接对我的数据库运行这个 sqlite 代码:

SELECT (title, ROW_NUMBER() OVER (ORDER BY title) AS placement) FROM note

我得到错误:near ",": syntax error:

标签: python-3.xpeewee

解决方案


您的 SQLite 版本可能不支持窗口函数,因为我相信这是最近在 3.25 中添加的。


推荐阅读