有一个sqlalchemy对象
class Employee(Base):
__tablename__ = "employee"
# Columns
id = Column(Integer, primary_key=True, autoincrement=True)
representative_id = Column(Integer, ForeignKey('employee.id'))
parent_id = Column(Integer, ForeignKey('employee.id'))
affiliate_id = Column(Integer, ForeignKey('employee.id'))
# Relatonships
representative = relationship("Employee", ????)
parent = relationship("Employee", ????)
affiliate = relationship("Employee", ????)
如果员工可以拥有0或1个家长,会员和代表,如何以正确的方式配置关系?知道这三个是不同的。 DB - MySQL
答案 0 :(得分:1)
我发现自己该怎么做:
class Employee(Base):
__tablename__ = "employee"
# Columns
id = Column(Integer, primary_key=True, autoincrement=True)
representative_id = Column(Integer, ForeignKey('employee.id'))
parent_id = Column(Integer, ForeignKey('employee.id'))
affiliate_id = Column(Integer, ForeignKey('employee.id'))
# Relatonships
representative = relationship("Employee",
primaryjoin="Employee.representative_id==Employee.id",
remote_side=[id])
parent = relationship("Employee",
primaryjoin="Employee.parent_id==Employee.id",
remote_side=[id])
affiliate = relationship("Employee",
primaryjoin="Employee.affiliate_id==Employee.id",
remote_side=[id])