2017-10-06 65 views
0

我有記錄的表,其中包含有關員工的各種信息 例如:你如何做多對多的桌子?

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) 

我現在使用的瓶-的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

Employee尚未與任何Log關係,所以使用帶有Log加盟,謂語是很難查詢Employee

但是,你可以簡單地查詢Logemployee_idskill_id作爲子查詢,並與給定的結果取Employee

# 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() 
+0

感謝您的幫助伊萬! 此外,我發佈了另一種方法,它在表模型級別實現它。 有空時檢查一下。 –

+0

感謝分享。我已經提到你的'你的員工沒有和Log建立任何關係......',這正是你的其他方法所倡導的。 –

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')