簡體   English   中英

SQLAlchemy的。 如何訂購多對多關系?

[英]SQLAlchemy. How to order on many to many relationship?

我有一個名為NoteType的SQLAlchemy模型,其關系名為sections NoteSection表被加入到NoteType通過表NoteTypeToSectionMap

我希望NoteType模型上的節列表由NoteTypeToSectionMap模型上的位置字段NoteTypeToSectionMap 我下面的代碼似乎隨機地在訂購各部分。

有人知道如何下訂單嗎?

謝謝!

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)

重新編寫關系,如下所示。

sections = db.relationship("NoteSection",
            secondary=NoteTypeToSectionMap.__table__,
            order_by=NoteTypeToSectionMap.__table__.c.position)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM