0

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人の親、アフィリエイト、および代表者を持つことができる場合、正しい方法で関係を構成するにはどうすればよいですか?これらの3つが異なるものであることを知ってください。DB-MySQL

4

1 に答える 1

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])
于 2012-05-23T17:09:32.777 に答える