SQL炼金术。如何在多对多关系上排序?
SQLAlchemy. How to order on many to many relationship?
我有一个名为 NoteType
的 SQLAlchemy 模型和一个名为 sections
的关系。 NoteSection
table 通过 NoteTypeToSectionMap
连接到 NoteType
table。
我希望 NoteType 模型上的部分列表按 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)
我有一个名为 NoteType
的 SQLAlchemy 模型和一个名为 sections
的关系。 NoteSection
table 通过 NoteTypeToSectionMap
连接到 NoteType
table。
我希望 NoteType 模型上的部分列表按 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)