orm - order_by() 方法在 peewee 中不起作用

标签 orm peewee

我正在使用带有简单节目-季节-剧集模式的 SQLite 后端:

class Show(BaseModel):
    name = CharField()

class Season(BaseModel):
    show = ForeignKeyField(Show, related_name='seasons')
    season_number = IntegerField()

class Episode(BaseModel):
    season = ForeignKeyField(Season, related_name='episodes')
    episode_number = IntegerField()

我需要以下查询:

seasons = (Season.select(Season, Episode)
       .join(Episode)
       .where(Season.show == SHOW_ID)
       .order_by(Season.season_number.desc(), Episode.episode_number.desc())
       .aggregate_rows())

SHOW_ID 是我想要剧季列表的节目的 ID。 但是当我使用以下代码遍历查询时:

for season in seasons:
    for episode in season.episodes:
        print(episode.episode_number)

...我得到的东西根本没有排序,甚至不符合我在不使用 order_by() 的情况下得到的顺序,即插入顺序。

我激活调试日志以查看传出查询,查询确实包含 ORDER BY 子句,手动应用它会返回正确的降序。

我是 peewee 的新手,我见过很多使用 join() 结合 order_by() 的例子,但我仍然找不到我做错了什么。

最佳答案

这是由于在聚合查询结果包装器中处理嵌套集合时出现错误。

github 问题是:https://github.com/coleifer/peewee/issues/519

修复已合并到此处:https://github.com/coleifer/peewee/commit/ec0e87f1a480695d98bf1f0d7f2e63aed8dfc440

因此,要获得修复,您需要克隆 master 或等到应该在下周或两周内发布的下一个版本 (2.4.7)。

关于orm - order_by() 方法在 peewee 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255182/

相关文章:

c# - asp.net mvc 5 SQL查询中的命名参数

python - 如何在 Peewee ORM 中使用 fn 对象

python - 使用 peewee 检查查询是否存在

python - Peewee MySQL 服务器已消失错误

java - hibernate 3 : Self-Referencing Objects

php - OO 中的后端应用程序 PHP : Structuring classes/tables efficiently

python - 如何使用 peewee db_url.connect() 生成带有 RetryOperationalError 的连接?

python - peewee实例匹配查询不存在

ruby-on-rails - 有没有Rails 方法来获取ActiveRecord::Relation 的所有关联对象?

php - Doctrine :扩展实体类