3

I'm trying to use the versioning recipe described on the sqlalchemy website (http://www.sqlalchemy.org/docs/orm/examples.html#versioned-objects) along with a multi-level inheritance model (Joined-Table inheritance)

Here are my declarative statements:

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 

Whenever I try to "create_all()" I get the following error: sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'tissue_history' and 'leucegene_tissue_history'.

Single level-inheritance works beautifully (ie: if I stop at "Tissue" and don't declare the "LeukemicTissue") but I really need a multi-level inheritance scheme to work..

Can anyone give me any pointers ?

Thanks !!

1 Answers1

0

Well, it looks like zzzeek just fixed the bug you described in this changeset. So just update the file on your side and it should work.

Note: Also, please note that you seem to be misusing super(...) in your code: you should use the class itself as the first parameter, not the base class:

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
Community
  • 1
  • 1
van
  • 74,297
  • 13
  • 168
  • 171