我有一个班级Revision
,Revision
有以下定义:
class Revision(db.Model):
id = db.Column(db.Unicode, primary_key=True)
text = db.Column(db.Unicode)
此外,每个Revision
可以拥有一个祖先,就像与另一个Revision
的关系一样。每个Revision
也应该跟踪任何后继者,即Revision
,它将它作为他们的祖先。我尝试了以下两种方法:
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.
答案 0 :(得分:4)
您想要的是adjacency list。 SQLAlchemy在其examples目录中有一个example。重要的部分是backref的remote_side
参数:
ancestorId = db.Column(db.Unicode, db.ForeignKey(id))
successors = db.relationship("Revision",
backref=db.backref("ancestor", remote_side=id),
lazy="dynamic")