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.










share|improve this question




















  • 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












  • @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

















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.










share|improve this question




















  • 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












  • @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















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. 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
















  • 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












  • @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










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














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)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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)





    share|improve this answer

























      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)





      share|improve this answer























        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)





        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        malan

        22211




        22211






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Coverage of Google Street View

            Full-time equivalent

            Surfing