我有一个日志表,其中包含有关员工的各种信息 例如:
class Log(Model):
division_id = Column(Integer, ForeignKey('division.id'), nullable=False)
division = relationship("Division")
employee_id = Column(Integer, ForeignKey("employee.id"), nullable=False)
employee = relationship("Employee")
skill_id = Column(Integer, ForeignKey("skill.id"), nullable=False)
skill = relationship("Skill")
message = Column(String, default='OK', nullable=False)
date = Column(DateTime, default=NowTime(), nullable=True)
员工和技能表如下所示:
class Employee(Model):
id = Column(Integer, primary_key=True)
name = Column(String, unique=True, nullable=False)
division_id = Column(Integer, ForeignKey('division.id'), nullable=False)
division = relationship("Division")
class Skill(Model):
id = Column(Integer, primary_key=True)
name = Column(String, unique=True, nullable=False)
我目前正在使用Flask-Appbuilder,我有一个技能视图,显示当前所选技能的所有日志。
class LogView(ModelView):
datamodel = SQLAInterface(Log)
list_columns = ['division', 'employee', 'skill', 'message', 'date']
show_template = 'appbuilder/general/model/show_cascade.html'
class SkillLogView(ModelView):
datamodel = SQLAInterface(Skill)
list_columns = ['name']
related_views = [LogView]
show_template = 'appbuilder/general/model/show_cascade.html'
在SkillLogView中,我还想显示具有此技能的员工姓名列表。
如何从与当前技能相关的日志中获取员工?
我不知道该怎么做,但我认为这可能是很多人的情况。问题是有3个表,而不是2个。
有超过2个表可以做多对多吗?
还是有另一种方法可以完成我想做的事情吗?
任何帮助表示感谢。
答案 0 :(得分:0)
您的<select name="country" id="country" class="form-control">
<option value="AF" {{$customer->country=='AF'?'selected':null}}>Afghanistan</option>
<option value="AX" {{$customer->country=='AX'?'selected':null}}>Aland Islands</option>
尚未与Employee
建立任何关系,因此使用Log
与谓词进行联接查询Employee
很困难。
但是,您只需使用Log
作为子查询查询Log
employee_id
,然后使用给定结果获取skill_id
。
Employee
答案 1 :(得分:0)
网上有一个例子可以做到这一点!
它有一个人桌,汽车桌,汽车所有权表。
我所要做的就是用员工代替员工,用汽车技能替代汽车所有权。
参考网址: Many-to-Many with association object and all relationships defined crashes on delete
更新代码:
class Log(Model):
division_id = Column(Integer, ForeignKey('division.id'), nullable=False)
division = relationship("Division")
employee_id = Column(Integer, ForeignKey("employee.id"), nullable=False)
employee = relationship("Employee", backref=backref('log', passive_deletes='all'))
skill_id = Column(Integer, ForeignKey("skill.id"), nullable=False)
skill = relationship("Skill", backref=backref('log', passive_deletes='all'))
message = Column(String, default='OK', nullable=False)
date = Column(DateTime, default=NowTime(), nullable=True)
class Skill(Model):
id = Column(Integer, primary_key=True)
name = Column(String, unique=True, nullable=False)
employees = relationship('Employee', secondary='log', backref='skill')
class Employee(Model):
id = Column(Integer, primary_key=True)
name = Column(String, unique=True, nullable=False)
division_id = Column(Integer, ForeignKey('division.id'), nullable=False)
division = relationship("Division")
skills = relationship('Skill', secondary='log', backref='employee')