3

我有一个类RevisionRevision具有以下定义:

class Revision(db.Model):
    id = db.Column(db.Unicode, primary_key=True)
    text = db.Column(db.Unicode)

此外,每个人都Revision 可以有一个祖先,就像与另一个人的关系一样Revision。每个Revision人还应该跟踪任何后继者,即以Revision它为祖先的s。我尝试了以下两种方法:

ancestorId = db.Column(db.Unicode, db.ForeignKey("revision.id"))
successors = db.relationship("Revision", backref="ancestor", lazy="dynamic")

抛出:

sqlalchemy.exc.ArgumentError: Revision.successors and back-reference Revision.ancestor are both of the same direction <symbol 'ONETOMANY>.  Did you mean to set remote_side on the many-to-one side ?

successors = db.relationship("Revision", backref="ancestor", lazy="dynamic")

抛出:

sqlalchemy.exc.ArgumentError: Could not determine join condition between parent/child tables on relationship Revision.successors.  Specify a 'primaryjoin' expression.  If 'secondary' is present, 'secondaryjoin' is needed as well.
4

1 回答 1

4

你想要的是所谓的邻接列表。SQLAlchemy在其示例目录中有一个示例。重要的部分是remote_sidebackref 的参数:

ancestorId = db.Column(db.Unicode, db.ForeignKey(id))
successors = db.relationship("Revision",
                             backref=db.backref("ancestor", remote_side=id),
                             lazy="dynamic")
于 2013-02-24T14:51:34.727 回答