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