What is wrong with my relationships in SQL Alchemy?

£可爱£侵袭症+ 提交于 2019-12-03 01:59:15

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!