StackOverflow和3个表之间的继承?可能?

时间:2013-10-02 05:06:24

标签: python inheritance sqlalchemy

情况:

Entity Clients
Entity Manufacturers
Entity Vendors

在某些情况下,客户是制造商或供应商。这很简单明了如何实施。但在某些情况下,客户端是 - 供应商和制造商,并且这两个表都继承自Clients表。

请告诉我是否可以组织代码(通过不同的类?),以便我有一个类对象,其中包含来自制造商类和制造商的所有其他字段。供应商类和 INSERT 发生时 - 我们将有3个插入(客户,供应商,制造商表)

我当前的代码:

class Client(Base):

    __tablename__ = "Clients"

    # Properties
    ID            = Column(BigInteger,   primary_key=True)
    name          = Column(VARCHAR(255), nullable=False)
    phone1        = Column(VARCHAR(40))
    phone2        = Column(VARCHAR(40))
    fax           = Column(VARCHAR(40))
    contactName   = Column(VARCHAR(40))
    contactEmail  = Column(VARCHAR(255))
    contactEmail  = Column(TEXT)
    url           = Column(VARCHAR(255))
    logo          = Column(VARCHAR(255))
    bannerHeader  = Column(VARCHAR(255))
    bannerDescription = Column(VARCHAR(255))
    bannerImage   = Column(VARCHAR(255))
    contentHeader = Column(VARCHAR(255))
    content       = Column(VARCHAR(1000))
    created       = Column(DateTime,     nullable=False, default=datetime.now())
    updated       = Column(DateTime)
    deleted       = Column(DateTime)
    extID         = Column(VARCHAR(255))
    searchImage   = Column(VARCHAR(255))

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

    # Constructor
    def __init__(self, name, extID=None ):
        self.extID = extID
        self.name  = name


class Manufacturer(Client):

    __tablename__ = "Manufacturers"

    # Properties
    ID             = Column(BigInteger,   ForeignKey('Clients.ID'), primary_key=True)
    isBrandedPage  = Column(SmallInteger, nullable=False, default=0)
    isMegaDropdown = Column(SmallInteger, nullable=False, default=0)

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

    def __repr__(self):
         return "<Manufacturer('%s',' %s')>" % (self.ID, self.name)

class Vendor(Client):

    __tablename__ = "Vendors"

    # Properties
    ID                      = Column(BigInteger,   ForeignKey('Clients.ID'), primary_key=True)
    token                   = Column(VARCHAR(20),  default=None)
    privateLabelDistributor = Column(SmallInteger, nullable=False, default=0)

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

0 个答案:

没有答案