【问题标题】:Peewee 3 - Python - Rank query resultsPeewee 3 - Python - 排名查询结果
【发布时间】:2020-01-01 18:00:02
【问题描述】:

我有一个带有注释(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.x peewee


    【解决方案1】:

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

    【讨论】:

    • 首先:非常感谢您的帮助,非常感谢。你是对的,我得出了同样的结论。我的 SQLite 版本是 3.11,但仅从 3.25 开始支持窗口函数。我正在运行 Linux Mint 18.3(完全更新)并且我已经尝试过 Mint 19.3(最新版本),但在这两种情况下 SQLite 版本都是
    • 等等... coleifer/peewee... 你是那个coleifer ??!!??抱歉 =) 非常感谢您的出色工作。 Peewee 绝对很棒!
    • 您可以尝试使用 python 驱动程序静态链接 sqlite 的自定义构建:github.com/coleifer/… -- 如果可用,peewee 将使用 pysqlite3。
    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多