在Flask应用程序上对自关联关系使用association_proxy时修复__init __()错误

时间:2019-03-25 23:25:51

标签: python sqlalchemy

我正在使用flask开发一个应用程序,该应用程序使用两个用户之间的自引用关联关系,但无法像尝试创建我得到的关联并 init ()错误消息,说我刚添加1时就向关联代理发送了2个参数。

我试图通过Mychel和Simon在CASSANDRA-6276上发布的对偶association_proxy来实现自引用关联,并且遇到了错误,然后我试图通过所发布的对偶关系来实现自引用关联。在https://groups.google.com/forum/#!topic/sqlalchemy/OeLrx74p5vY上通过SingleNegationElimination遇到了KeyError,但没有足够的信息来调试


class tutorship(db.Model):
    __tablename__ = 'tutorship_tablename'
    """Reference table of tutors and students"""
    id = db.Column(db.Integer, primary_key = True)
    studentId = db.Column(db.Integer, db.ForeignKey('user.id'))
    professorId = db.Column(db.Integer, db.ForeignKey('user.id'))
    subject = db.Column(db.String(150), nullable = False)
    date = db.Column(db.DateTime, nullable = False)
    rating = db.Column(db.Float, nullable = True)
    timeAgreed = db.Column(db.Float, nullable = False)
    comments = db.Column(db.Text, nullable = True)

    # Original try
    student = db.relationship('User', primaryjoin=(studentId == User.id), backref='to_student')
    professor = db.relationship('User', primaryjoin=(professorId == User.id), backref='from_professor')

class User(db.Model, UserMixin):
    __tablename__='user'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(15), nullable=False)
    surname = db.Column(db.String(20), nullable=False)

    # Original try
    tutors = association_proxy('to_student', 'professor')
    students = association_proxy('from_professor', 'student')

        # Second try
    #students = db.relationship('tutorship', primaryjoin=(id == tutorship.studentId), backref='to_student')
    #professors = db.relationship('tutorship', primaryjoin=(id == tutorship.professorId), backref='from_professor')

@app.route('/<professorId>/<studentId>/<subject>/<date>/<timeAgreed>')
def tutorshipConfirmed(professorId, studentId, subject, date, timeAgreed):
    if (current_user.is_authenticated) and (str(current_user.id) == professorId):
        student = User.query.filter_by(id = int(studentId)).first()
        professor = User.query.filter_by(id = int(professorId)).first()
        date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        # Original try
        student.tutors.append(professor)

        # Second try
        #student.professors.append(professor)
        db.session.commit()
    return redirect(url_for('home'))

我希望在两个用户之间创建一个自引用关联的实例,但是在尝试时遇到了这些错误。

0 个答案:

没有答案