在sqlalchemy中使用版本控制和多级继承时出现问题



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

以下是我的声明:

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"one_answers"leuegene_tissue_history"之间找不到任何外键关系。

单级遗传效果很好(即:如果我在"组织"处停下来,不申报"白血病组织"),但我真的需要一个多级遗传方案才能发挥作用。。

有人能给我什么建议吗?

谢谢!!

看起来zzzeek刚刚修复了您在这个变更集中描述的错误。所以只要更新你这边的文件就可以了。

注意: 此外,请注意,您在代码中似乎滥用了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

最新更新