Sort by Count of Many to Many Relationship - SQLAlchemy

北慕城南 提交于 2019-12-06 06:40:25

Code below would work for the model you describe:

q = (db.session.query(Title, func.count(names_users.c.user_id).label("total"))
        .filter(Title.name.ilike(searchstring))
        .outerjoin(names_users).group_by(Title).order_by('total DESC')
    )
for x in q:
    print(x)

Your error, however, includes some other data like publisher or like. So if above is not helpful, you should add more information to your question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!