2016-02-08 61 views
2

我有一個金字塔應用程序中使用的SQLAlchemy以下模型:訂購SQLAlchemy的關係,而不是通過主鍵

class Issue(base_iop): 
    __tablename__ = 'issues' 
    issueid = Column(BIGINT, primary_key=True, name="ISSUEID") 
    issuenum = Column(VARCHAR(20), name="ISSUENUM") 
    status = Column(VARCHAR(32), name="STATUS") 
    datetime = Column(TIMESTAMP, name="ISSUETIME") 

class Related(base_iop): 
    __tablename__ = 'related' 
    relationid = Column(BIGINT, primary_key=True, name="ROWSTAMP") 
    parent_num = Column(VARCHAR(20), ForeignKey('issue.ISSUENUM'),name="RECORDKEY") 
    children_num = Column(VARCHAR(20), ForeignKey('issue.ISSUENUM'), name="RELATEDKEY") 
    issues = relationship(iop, foreign_keys=[child_num]) 

我能得到就好使用問題的相關問題:的issues屬性相關表格:

for related in db.query(Issue).all()[0].issues: 
    print related.status 

但是,我沒有找到解決方案來訂購datetime屬性的問題。如果Related已經datetime屬性就是這樣,它應該使用ORDER_BY的關係是非常簡單的:

class Issue(base_iop): 
    __tablename__ = 'issues' 
    issueid = Column(BIGINT, primary_key=True, name="ISSUEID") 
    issuenum = Column(VARCHAR(20), name="ISSUENUM") 
    status = Column(VARCHAR(32), name="STATUS") 
    datetime = Column(TIMESTAMP, name="ISSUETIME") 
    children = relationship("Related", foreign_keys="[Related.parent_num]", backref="parent", order_by="[Related.datetime]") 

class Related(base_iop): 
    __tablename__ = 'related' 
    relationid = Column(BIGINT, primary_key=True, name="ROWSTAMP") 
    parent_num = Column(VARCHAR(20), ForeignKey('issue.ISSUENUM'),name="RECORDKEY") 
    children_num = Column(VARCHAR(20), ForeignKey('issue.ISSUENUM'), name="RELATEDKEY") 
    datetime = Column(TIMESTAMP, name="ISSUETIME") 
    issues = relationship(iop, foreign_keys=[child_num]) 

如何通過另一場責令相關問題,而不是主鍵就像是現在?

+0

當您db.query相關這麼做確認....你想在日期時間的順序要打印的realted問題?你能改變桌子的設計嗎?比如這是一個新項目還是現有的大量數據? – crooksey

回答