SQLAlchemy - order_by on relationship for join table

ε祈祈猫儿з 提交于 2019-12-03 10:43:55
Nick Woodhams

I couldn't make any of these solutions work, however I found an easier way.

from sqlalchemy.ext.declarative import declarative_base

class User(Base):
    # ....
    addresses = relationship("Address",
                         order_by="desc(Address.email)",
                         primaryjoin="Address.user_id==User.id")

Found here: http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/relationships.html

What you want is to order the role attribute of the RolePermission object. Passing order_by sets the order in the Role class.

Try this:

from sqlalchemy.orm import backref

permissionLinks = relationship(RolePermission, backref=backref("role", order_by=name))

setting an order for the back reference

The problem is in permissionLinks relationship. Ordering its items by by Role.name makes no sense to me.

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