首页 > 解决方案 > 无法确定关系上的父/子表之间的连接条件

问题描述

我有以下 sqlite3 数据库 Sqlite 数据库

我想将上面的表格映射到 python 代码。我的代码是

class Attendance(db.Model):
    Id = db.Column(db.Integer, primary_key=True)
    day = db.Column(db.Date)
    employee = db.Column(db.Text)

    def __init__(self, day,employee):
        self.day = day
        self.employee = employee

   def __repr__(self):
       return '<Employee %r>' % self.employee

class AttendanceActions(db.Model):
    Id = db.Column(db.Integer, primary_key=True)
    attendance_id = db.Column(db.Integer, db.ForeignKey('Attendance.Id'))
    attendance = db.relationship('Attendance', backref='AttendanceActions', lazy='dynamic')
    action_time = db.Column(db.DateTime)
    action = db.Column(db.Text)

    def __init__(self, attendance, action_time, action):
       self.attendance = attendance
       self.action_time = action_time
       self.action = action

    def __repr__(self):
       return '<AttendanceAction %r>' % self.action_time

我得到了错误:

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship AttendanceActions.attendance - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

标签: pythonsqliteflasksqlalchemy

解决方案


推荐阅读