What is wrong with my relationships in SQL Alchemy?

后端 未结 2 1158
无人共我
无人共我 2021-02-05 10:34

I am using SQLAlchemy with Flask to create relationships for my application. I recently rewrote the relationships, and, no matter what I change, I keep getting the error:

相关标签:
2条回答
  • This error:

    Could not determine join condition between parent/child tables on relationship CurriculumVersion.enrollments 
    

    means that SQLAlchemy could not find a proper column in Enrollments to use as the foreign key in the relationship.

    You defined the foreign key, but you used an incorrect table name. Flask-SQLAlchemy converts CamelCase classes to camel_case when creating the table, so you need to change this:

    class Enrollment(db.Model, AuthUser):
        # ...
        version_id = db.Column(db.Integer, db.ForeignKey('curriculumversion.id'))
        #...
    

    to this:

    class Enrollment(db.Model, AuthUser):
        # ...
        version_id = db.Column(db.Integer, db.ForeignKey('curriculum_version.id'))
        #...
    

    Alternatively you can use the __tablename__ attribute to override the default naming convention used by Flask-SQLAlchemy.

    0 讨论(0)
  • 2021-02-05 11:23

    Try to use primaryjoin in your CurriculumVersion class as follows:

    Change

    enrollments = db.relationship('Enrollment', backref='enrollment', lazy='dynamic')
    

    to

    enrollments = db.relationship('Enrollment', backref='enrollment', lazy='dynamic', primaryjoin="Enrollment.version_id==CurriculumVersion.id")
    

    Note: You might need to do this for the other classes as well.

    0 讨论(0)
提交回复
热议问题