0

我有一个日志表,其中包含有关员工 ex 的各种信息:

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 张桌子做多对多?

还是有另一种方法来完成我想做的事情?

任何帮助表示赞赏。

4

2 回答 2

0

Employee尚未与 建立任何关系Log,因此Employee使用 join with查询Log谓词很困难。

但是,您可以简单地将Logfor作为子查询进行查询,并employee_id使用给定的结果进行获取。skill_idEmployee

# Subquery returns `employee_id` from `logs` with the given `skill_id`
sq = session.query(Log.employee_id).\
    filter(Log.skill_id == skill_id).\
    subquery()

# Fetch `Employee` that matches `employee_id` in `sq`
q = session.query(Employee).\
    filter(Employee.employee_id.in_(sq))

employees = q.all()  # or paginate, e.g. q.limit(..).all()
于 2017-10-06T03:30:32.580 回答
0

网上有一个例子可以做到这一点!

它有一个人表,汽车表,汽车所有权表。

我所要做的就是用员工代替人,用技能代替汽车,用日志代替汽车所有权。

参考 URL: 具有关联对象的多对多和定义的所有关系在删除时崩溃

更新代码:

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')
于 2017-10-06T04:32:08.307 回答