时间:2011-06-16 16:04:48

标签: inheritance sqlalchemy database-versioning

我正在尝试使用sqlalchemy网站(http://www.sqlalchemy.org/docs/orm/examples.html#versioned-objects)上描述的版本控制配方以及多级继承模型(Joined-Table继承)

以下是我的声明性陈述:

class Sample(Base):
    __metaclass__ = VersionedMeta
    __tablename__ = 'sample'
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, primary_key=True)
    discriminator = Column('type', String(50))
    token = Column(String(128), nullable=False)
    source_sample_id = Column(Integer, ForeignKey('test.sample.id'))

    children = relationship("Sample", backref=backref('source_sample', remote_side=id), single_parent=True)

    __mapper_args__ = {'polymorphic_on': discriminator, 'polymorphic_identity':'sample'}

    def __init__(self, token, source_sample_id=None):
        self.token = token
        self.source_sample_id = source_sample_id

class Tissue(Sample):
    __metaclass__ = VersionedMeta
    __tablename__ = 'tissue'
    __mapper_args__ = {'polymorphic_identity': 'tissue'}
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, ForeignKey('test.sample.id'), primary_key=True)
    concentration = Column(String(32))

    def __init__(self, token, concentration, source_sample_id=None):
        super(Sample, self).__init__(token, source_sample_id)
        self.concentration = concentration

class LeukemicTissue(Tissue):
    __metaclass__ = VersionedMeta
    __tablename__ = 'leukemic_tissue'
    __mapper_args__ = {'polymorphic_identity': 'leukemic_tissue'}
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, ForeignKey('test.tissue.id'), primary_key=True)
    leukemia = Column(String)

    def __init__(self, token, concentration, leukemia, source_sample_id=None):
        super(Tissue, self).__init__(token, concentration, source_sample_id)
        self.leukemia = leukemia 

每当我尝试“create_all()”时,我都会收到以下错误: sqlalchemy.exc.ArgumentError:找不到'tissue_history'和'leucegene_tissue_history'之间的任何外键关系。

单级继承工作得很漂亮(即:如果我停在“Tissue”并且没有声明“LeukemicTissue”),但我真的需要一个多级继承方案来工作..

任何人都可以给我任何指示吗?

谢谢!!

1 个答案:

答案 0 :(得分:0)

好吧,看起来zzzeek只修复了您在此changeset中描述的错误。因此,只需更新您身边的文件即可。

注意: 另外,请注意您似乎在代码中滥用super(...):您应该将类​​本身用作第一个参数,而不是基类:

class LeukemicTissue(Tissue):
    # ...
    def __init__(self, token, concentration, leukemia, source_sample_id=None):
        #super(Tissue, self).__init__(token, concentration, source_sample_id) # ERROR
        super(LeukemicTissue, self).__init__(token, concentration, source_sample_id) # GOOD