Sqlalchemy嵌套类层次结构

时间:2017-08-22 01:38:14

标签: python postgresql sqlalchemy entity-attribute-value

在sqlalchemy中使用Relationships with Joined Inheritance时,我有一个复杂的类层次结构,你可以在这个模型中看到,它来自以下模式:Entity Attribute Value以及下面的复杂图表:Entities and Classifications

这个型号是否正确?我在哪里添加这些模型的关系?我该如何重写这个模型?

class Party(Base):
    __tablename__ = 'party'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    type = Column(String(50))

    __mapper_args__ = {
        'polymorphic_identity':'party',
        'polymorphic_on':type
    }

class Organisation(Party):
    __tablename__ = 'organisation'
    id = Column(Integer, ForeignKey('party.id'), primary_key=True)
    type = Column(String(50))
    company_id = Column(ForeignKey('party.id'))
    company = relationship("Company", back_populates="managers")

    __mapper_args__ = {
        'polymorphic_identity':'organisation',
    'polymorphic_on':type
    }

class Legal():
    __tablename__ = 'legal' 
    id = Column(Integer, ForeignKey('organisation.id'), primary_key=True)   
    name = Column(String(50))
    tax_id = Column(String(50))
    __mapper_args__ = {
        'polymorphic_identity':'legal',
    }

class Informal():
    __tablename__ = 'informal'  
    id = Column(Integer, ForeignKey('organisation.id'), primary_key=True)   
    name = Column(String(50))
    tax_id = Column(String(50))
    __mapper_args__ = {
        'polymorphic_identity':'informal',
    }



class Person(Party):
    __tablename__ = 'party'
    id = Column(Integer, ForeignKey('party.id'), primary_key=True)
    engineer_name = Column(String(30))

    __mapper_args__ = {
        'polymorphic_identity':'person',
    }

0 个答案:

没有答案