我想写用flask和flsak-sqlalchemy写一个日志记录系统,接待日志表、故障日志表、会议日志表关联文件表,实现建立前面三个日志表之一时,能同时上传多个附件,并且故障日志表关联了进度事件表,能够在故障日志表中插入多个进度事件,我写的数据库模型代码如下:
class ReceptionLog(db.Model):
# 接待日志表
id = db.Column(db.Integer, primary_key=True) # id
title = db.Column(db.String(100)) # 标题
startDate = db.Column(db.DateTime, nullable=False) # 开始日期
startTime = db.Column(db.DateTime, nullable=False) # 开始时间
endDate = db.Column(db.DateTime) # 结束日期
endTime = db.Column(db.DateTime) # 结束时间
guestWorkUnit = db.Column(db.String(100)) # 来宾单位
guestName = db.Column(db.String(100)) # 来宾姓名
guestTitle = db.Column(db.String(100)) # 来宾职务
guestNumber = db.Column(db.Integer) # 来宾人数
accompanyPerson = db.Column(db.String(100)) # 接待人员
accompanyTitle = db.Column(db.String(100)) # 接待人员职务
accompanyNumber = db.Column(db.Integer) # 陪同人数
accompWorkUnit = db.Column(db.String(100)) # 接待单位
commentator = db.Column(db.String(100)) # 解说人员
support = db.Column(db.String(100)) # 保障人员
supportNumber = db.Column(db.Integer) # 保障人数
remark = db.Column(db.String(100)) # 备注
files = db.relationship('File', back_populates='receptionLog')
class FaultLog(db.Model):
# 故障日志表
id = db.Column(db.Integer, primary_key=True) #日志id
title = db.Column(db.String(100)) # 标题
startDate = db.Column(db.DateTime, nullable=False) # 开始日期
startTime = db.Column(db.DateTime, nullable=False) # 开始时间
endDate = db.Column(db.DateTime) # 结束日期
endTime = db.Column(db.DateTime) # 结束时间
files = db.relationship('File', back_populates='faultLog')
events = db.relationship('Event', back_populates='faultLog')
class MeetingLog(db.Model):
# 会议日志表
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100)) # 标题
startDate = db.Column(db.DateTime, nullable=False) # 开始日期
startTime = db.Column(db.DateTime, nullable=False) # 开始时间
endDate = db.Column(db.DateTime, nullable=False) # 结束日期
endTime = db.Column(db.DateTime, nullable=False) # 结束时间
files = db.relationship('File', back_populates='meetingLog')
class File(db.Model):
# 文件表
id = db.Column(db.Integer, primary_key=True) # 文件id
title = db.Column(db.String(100)) # 标题
date = db.Column(db.DateTime, nullable=False) # 文件日期
time = db.Column(db.DateTime, nullable=False) # 文件时间
path = db.Column(db.String(255)) # 文件路径
receptionLog_id = db.Column(db.Integer, db.ForeignKey('receptionLog.id')) # 设置为接待日志的外键
faultLog_id = db.Column(db.Integer, db.ForeignKey('faultLog.id')) # 设置为故障日志的外键
meetingLog_id = db.Column(db.Integer, db.ForeignKey('meetingLog.id')) # 设置为会议日志的外键
receptionLog = db.relationship('ReceptionLog', back_populates='files') # 与接待日志关联, 一对多
faultLog = db.relationship('FaultLog', back_populates='files') # 与故障日志关联, 一对多
meetingLog = db.relationship('MeetingLog', back_populates='files') # 与会议日志关联, 一对多
class Event(db.Model):
# 故障进度记录
id = db.Column(db.Integer, primary_key=True) # id
time = db.Column(db.DateTime, nullable=False) # 时间
description = db.Column(db.String(255)) # 具体情况
faultLog_id = db.Column(db.Integer, db.ForeignKey('faultLog.id')) # 设置为故障日志的外键
faultLog = db.relationship('FaultLog', back_populates='events') # 与故障日志关联, 一对多
在执行上面的数据库模型代码时,遇到以下报错:
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship ReceptionLog.files - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a ‘primaryjoin’ expression.