OneToMany Elixir 关系的按计数排序
我正在使用 Elixir 进行 ORM,但在尝试按关系排序时遇到问题。
我想做的是获取按帖子数量排序的用户列表。我尝试过诸如此类的方法,但
User.query.join(User.posts).order_by(func.count(User.posts)).all()
没有成功。
这是我的 Elixir 实体:
class User(Entity):
username = Field(Unicode(100))
posts = OneToMany('Post', inverse='user')
class Post(Entity):
content = Field(Unicode(20000))
user = ManyToOne('User')
I am using Elixir for ORM but I have a problem trying to Order by a Relationship.
What I am trying to do is to get a list of users sorted by the number of posts they have. I have tried ways such as
User.query.join(User.posts).order_by(func.count(User.posts)).all()
without any success.
Here are my Elixir entities:
class User(Entity):
username = Field(Unicode(100))
posts = OneToMany('Post', inverse='user')
class Post(Entity):
content = Field(Unicode(20000))
user = ManyToOne('User')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个常见问题,ORM 教程中的示例查询位于:http://www. sqlalchemy.org/docs/orm/tutorial.html#using-subqueries 。只需将 order_by(User.id) 更改为 order_by(stmt.c.address_count) 即可:
this is a common question and an example query is in the ORM tutorial at: http://www.sqlalchemy.org/docs/orm/tutorial.html#using-subqueries . Just change where it says order_by(User.id) to say order_by(stmt.c.address_count):