【发布时间】: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