SQLAlchemy - order_by on relationship for join table

后端 未结 3 1558
灰色年华
灰色年华 2021-02-12 20:24

I\'m using declarative SQLAlchemy and I have three models: Role, Permission, and RolePermission. In my Role model, I have t

3条回答
  •  梦毁少年i
    2021-02-12 20:36

    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

提交回复
热议问题