python - SQLAlchemy. How to order on many to many relationship? -
i have sqlalchemy model named notetype relationship named sections. notesection table joined notetype table through notetypetosectionmap.
i want sections list on notetype model ordered position field on notetypetosectionmap model. code have below seems randomly ordering sections.
does know how ordering work?
thanks!
class notetype(modelabstract): __tablename__ = "notetype" id = db.column(db.integer, primary_key=true) name = db.column(db.string(255)) description = db.column(db.string(255)) sections = db.relationship("notesection", secondary=notetypetosectionmap.__table__, primaryjoin=id==notetypetosectionmap.__table__.c.notetypeid, secondaryjoin=id==notetypetosectionmap.__table__.c.notesectionid, order_by=notetypetosectionmap.__table__.c.position) -
class notetypetosectionmap(modelabstract): __tablename__ = "notetypetosectionmap" id = db.column(db.integer, primary_key=true) notetypeid = db.column(db.integer, db.foreignkey("notetype.id")) notesectionid = db.column(db.integer, db.foreignkey("notesection.id")) position = db.column(db.integer)
re-write relationship follows.
sections = db.relationship("notesection", secondary=notetypetosectionmap.__table__, order_by=notetypetosectionmap.__table__.c.position)
Comments
Post a Comment