Sqlalchemy - double polymorphic inheritance issue

I have a class mapping with two polymorphic inheritances:

#test classes

class AbstractA(Base):

    __tablename__ = "abstract_a"

    id = Column(Integer, primary_key=True)
    class_name = Column('class_name', String(50))

    __mapper_args__ = {
      'polymorphic_on': class_name,
    }


#some stuff here

class AbstractB(AbstractA):

    __tablename__ = "abstract_b"

    id = Column(Integer, ForeignKey('abstract_a.id'), primary_key=True)
    class_name = Column('class_name', String(50))

    __mapper_args__ = {
      'polymorphic_on': class_name,
      'polymorphic_identity': __tablename__,
    }

#some stuff here

class ClassA(AbstractB):

    __tablename__ = "table_a"
    __mapper_args__ = {
      'polymorphic_identity': __tablename__,
    }

    id = Column(Integer, ForeignKey('abstract_b.id'), primary_key=True)
    label = Column('label', String(50))

    def __init__(self, label):
        self.label = label

I save the ClassA object:

object = ClassA('toto')
db_session.add(object)
db_session.commit()

When I try to reload the object as follows:

reloaded_object = AbstractB.query.first()

I am returning a ClassA object (just fine)

but when I try to reboot like this:

reloaded_object = AbstractA.query.first()

I am returning an AbstractA object because abstract_a.class_name has not been set to polyorphic_identity.

Is this a problem or an expected job?

+3
source share
1 answer

AbstractB AbstractA.class_name ? 2 AbstractB, :

class AbstractB(AbstractA):

    __tablename__ = "abstract_b"

    id = Column(ForeignKey('abstract_a.id'), primary_key=True)

    __mapper_args__ = {
        'polymorphic_identity': __tablename__,
    }
+1

Source: https://habr.com/ru/post/1759735/


All Articles