Two arithmetical operations in primaryjoin and_ causes failure
up vote
0
down vote
favorite
I have a versioning system of annotations
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))
I have the following two relationships on the Annotation class:
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)
As you can see, the context is simply the first_line_num-5 and last_line_num+5; in other words, the context of the annotation is simply the the prior five and next five lines to the actual body of the text of the annotation.
I am trying to define the same context relationship on the actual 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)
But this exact definition always returns a failure of
sqlalchemy.exc.ArgumentError: Could not locate any relevant foreign
key columns for primary join condition 'line.l_num >=
annotation_version.first_line_num - :first_line_num_1 AND line.l_num
<= annotation_version.last_line_num + :last_line_num_1 AND
line.book_id = annotation_version.book_id' on relationship
AnnotationVersion.context. Ensure that referencing columns are
associated with a ForeignKey or ForeignKeyConstraint, or are annotated
in the join condition with the foreign() annotation.
If I remove either the +5 or the -5 it works. But as soon as I define both, I get that error.
What on earth could cause this particular failure? As you can see it only happens when defined in the primaryjoin condition, because it works perfectly as a secondaryjoin condition.
python sqlalchemy
add a comment |
up vote
0
down vote
favorite
I have a versioning system of annotations
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))
I have the following two relationships on the Annotation class:
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)
As you can see, the context is simply the first_line_num-5 and last_line_num+5; in other words, the context of the annotation is simply the the prior five and next five lines to the actual body of the text of the annotation.
I am trying to define the same context relationship on the actual 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)
But this exact definition always returns a failure of
sqlalchemy.exc.ArgumentError: Could not locate any relevant foreign
key columns for primary join condition 'line.l_num >=
annotation_version.first_line_num - :first_line_num_1 AND line.l_num
<= annotation_version.last_line_num + :last_line_num_1 AND
line.book_id = annotation_version.book_id' on relationship
AnnotationVersion.context. Ensure that referencing columns are
associated with a ForeignKey or ForeignKeyConstraint, or are annotated
in the join condition with the foreign() annotation.
If I remove either the +5 or the -5 it works. But as soon as I define both, I get that error.
What on earth could cause this particular failure? As you can see it only happens when defined in the primaryjoin condition, because it works perfectly as a secondaryjoin condition.
python sqlalchemy
1
I think you should read docs.sqlalchemy.org/en/latest/orm/…. At least it is explained that determining "foreign" columns is unique to primary join, which explains why your secondary join is working. On a glance your primary does not contain any predicates involving a foreign key that would (directly) connect the involved tables / models. InLine.book_id==AnnotationVersion.book_idboth are foreign keys to a 3rd tablebook.
– Ilja Everilä
Nov 12 at 10:43
@IljaEverilä thank you, I'll go ahead and write the answer, as that solved it. All I have to do is enterforeign_keys=[first_line_num, last_line_num].
– malan
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a versioning system of annotations
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))
I have the following two relationships on the Annotation class:
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)
As you can see, the context is simply the first_line_num-5 and last_line_num+5; in other words, the context of the annotation is simply the the prior five and next five lines to the actual body of the text of the annotation.
I am trying to define the same context relationship on the actual 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)
But this exact definition always returns a failure of
sqlalchemy.exc.ArgumentError: Could not locate any relevant foreign
key columns for primary join condition 'line.l_num >=
annotation_version.first_line_num - :first_line_num_1 AND line.l_num
<= annotation_version.last_line_num + :last_line_num_1 AND
line.book_id = annotation_version.book_id' on relationship
AnnotationVersion.context. Ensure that referencing columns are
associated with a ForeignKey or ForeignKeyConstraint, or are annotated
in the join condition with the foreign() annotation.
If I remove either the +5 or the -5 it works. But as soon as I define both, I get that error.
What on earth could cause this particular failure? As you can see it only happens when defined in the primaryjoin condition, because it works perfectly as a secondaryjoin condition.
python sqlalchemy
I have a versioning system of annotations
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))
I have the following two relationships on the Annotation class:
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)
As you can see, the context is simply the first_line_num-5 and last_line_num+5; in other words, the context of the annotation is simply the the prior five and next five lines to the actual body of the text of the annotation.
I am trying to define the same context relationship on the actual 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)
But this exact definition always returns a failure of
sqlalchemy.exc.ArgumentError: Could not locate any relevant foreign
key columns for primary join condition 'line.l_num >=
annotation_version.first_line_num - :first_line_num_1 AND line.l_num
<= annotation_version.last_line_num + :last_line_num_1 AND
line.book_id = annotation_version.book_id' on relationship
AnnotationVersion.context. Ensure that referencing columns are
associated with a ForeignKey or ForeignKeyConstraint, or are annotated
in the join condition with the foreign() annotation.
If I remove either the +5 or the -5 it works. But as soon as I define both, I get that error.
What on earth could cause this particular failure? As you can see it only happens when defined in the primaryjoin condition, because it works perfectly as a secondaryjoin condition.
python sqlalchemy
python sqlalchemy
edited Nov 10 at 20:42
asked Nov 10 at 14:27
malan
22211
22211
1
I think you should read docs.sqlalchemy.org/en/latest/orm/…. At least it is explained that determining "foreign" columns is unique to primary join, which explains why your secondary join is working. On a glance your primary does not contain any predicates involving a foreign key that would (directly) connect the involved tables / models. InLine.book_id==AnnotationVersion.book_idboth are foreign keys to a 3rd tablebook.
– Ilja Everilä
Nov 12 at 10:43
@IljaEverilä thank you, I'll go ahead and write the answer, as that solved it. All I have to do is enterforeign_keys=[first_line_num, last_line_num].
– malan
2 days ago
add a comment |
1
I think you should read docs.sqlalchemy.org/en/latest/orm/…. At least it is explained that determining "foreign" columns is unique to primary join, which explains why your secondary join is working. On a glance your primary does not contain any predicates involving a foreign key that would (directly) connect the involved tables / models. InLine.book_id==AnnotationVersion.book_idboth are foreign keys to a 3rd tablebook.
– Ilja Everilä
Nov 12 at 10:43
@IljaEverilä thank you, I'll go ahead and write the answer, as that solved it. All I have to do is enterforeign_keys=[first_line_num, last_line_num].
– malan
2 days ago
1
1
I think you should read docs.sqlalchemy.org/en/latest/orm/…. At least it is explained that determining "foreign" columns is unique to primary join, which explains why your secondary join is working. On a glance your primary does not contain any predicates involving a foreign key that would (directly) connect the involved tables / models. In
Line.book_id==AnnotationVersion.book_id both are foreign keys to a 3rd table book.– Ilja Everilä
Nov 12 at 10:43
I think you should read docs.sqlalchemy.org/en/latest/orm/…. At least it is explained that determining "foreign" columns is unique to primary join, which explains why your secondary join is working. On a glance your primary does not contain any predicates involving a foreign key that would (directly) connect the involved tables / models. In
Line.book_id==AnnotationVersion.book_id both are foreign keys to a 3rd table book.– Ilja Everilä
Nov 12 at 10:43
@IljaEverilä thank you, I'll go ahead and write the answer, as that solved it. All I have to do is enter
foreign_keys=[first_line_num, last_line_num].– malan
2 days ago
@IljaEverilä thank you, I'll go ahead and write the answer, as that solved it. All I have to do is enter
foreign_keys=[first_line_num, last_line_num].– malan
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Ilja Everila's reference to the documentation helped solve it. All I had to do was specify the foreign_key param:
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)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Ilja Everila's reference to the documentation helped solve it. All I had to do was specify the foreign_key param:
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)
add a comment |
up vote
0
down vote
accepted
Ilja Everila's reference to the documentation helped solve it. All I had to do was specify the foreign_key param:
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)
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Ilja Everila's reference to the documentation helped solve it. All I had to do was specify the foreign_key param:
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)
Ilja Everila's reference to the documentation helped solve it. All I had to do was specify the foreign_key param:
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)
answered 2 days ago
malan
22211
22211
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239914%2ftwo-arithmetical-operations-in-primaryjoin-and-causes-failure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I think you should read docs.sqlalchemy.org/en/latest/orm/…. At least it is explained that determining "foreign" columns is unique to primary join, which explains why your secondary join is working. On a glance your primary does not contain any predicates involving a foreign key that would (directly) connect the involved tables / models. In
Line.book_id==AnnotationVersion.book_idboth are foreign keys to a 3rd tablebook.– Ilja Everilä
Nov 12 at 10:43
@IljaEverilä thank you, I'll go ahead and write the answer, as that solved it. All I have to do is enter
foreign_keys=[first_line_num, last_line_num].– malan
2 days ago