我有注释的版本控制系统
class Annotation(db.Model):
id = db.Column(db.Integer, primary_key=True)
class AnnotationVersion(db.Model):
id = db.Column(db.Integer, primary_key=True)
book_id = db.Column(db.Integer, db.ForeignKey("book.id"))
previous_id = db.Column(db.Integer, db.ForeignKey("post_version.id"), default=None)
pointer_id = db.Column(db.Integer, db.ForeignKey("annotation.id"))
current = db.Column(db.Boolean, index=True)
first_line_num = db.Column(db.Integer)
last_line_num = db.Column(db.Integer)
class Line(db.Model):
id = db.Column(db.Integer, primary_key=True)
book_id = db.Column(db.Integer, db.ForeignKey("book.id")
line = db.Column(db.String(255))
我在Annotation
类上具有以下两种关系:
lines = db.relationship("Line", secondary="annotation_version",
primaryjoin="and_(Annotation.id==AnnotationVersion.pointer_id,"
"AnnotationVersion.current==True)",
secondaryjoin="and_(Line.l_num>=AnnotationVersion.first_line_num,"
"Line.l_num<=AnnotationVersion.last_line_num,"
"Line.book_id==AnnotationVersion.book_id)",
viewonly=True, uselist=True)
context = db.relationship("Line", secondary="annotation_version",
primaryjoin="and_(Annotation.id==AnnotationVersion.pointer_id,"
"AnnotationVersion.current==True)",
secondaryjoin="and_(Line.l_num>=AnnotationVersion.first_line_num-5,"
"Line.l_num<=AnnotationVersion.last_line_num+5,"
"Line.book_id==AnnotationVersion.book_id)",
viewonly=True, uselist=True)
如您所见,上下文仅仅是first_line_num-5
和last_line_num+5
;换句话说,注释的上下文就是注释文本实际正文的前五行和后五行。
我正在尝试在实际的AnnotationVersion
上定义相同的上下文关系:
context = db.relationship("Line",
primaryjoin="and_(Line.l_num>=AnnotationVersion.first_line_num-5,"
"Line.l_num<=AnnotationVersion.last_line_num+5,"
"Line.book_id==AnnotationVersion.book_id)",
viewonly=True, uselist=True)
但是这个确切的定义总是返回失败
sqlalchemy.exc.ArgumentError:无法找到任何相关的外部文件 主要联接条件'line.l_num> =的关键列 注解版本.first_line_num-:first_line_num_1和line.l_num <=注解version.last_line_num +:last_line_num_1 AND line.book_id =注解version.book_id'关于关系 AnnotationVersion.context。确保引用列是 与ForeignKey或ForeignKeyConstraint关联,或被注释 在带有foreign()注释的连接条件中。
如果我删除了+5
或-5
,它将起作用。但是,一旦我同时定义了两者,就会收到该错误。
到底是什么原因导致此特殊故障?如您所见,它仅在primaryjoin
条件中定义时才会发生,因为它完美地用作secondaryjoin
条件。
答案 0 :(得分:0)
Ilja Everila对文档的引用帮助解决了它。我要做的就是指定foreign_key
参数:
context = db.relationship("Line",
primaryjoin="and_(Line.l_num>=AnnotationVersion.first_line_num-5,"
"Line.l_num<=AnnotationVersion.last_line_num+5,"
"Line.book_id==AnnotationVersion.book_id)",
foreign_keys=[first_line_num, last_line_num],
viewonly=True, uselist=True)