Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You definitely can do this. The code below is using the <code>declarative_base</code>, but shows the model setup which works. <code>D</code> class is both a parent and a child using class inheritance. However, the <code>polymorphic_identity</code> is stored only on the top level. Make sure you have all proper foreign keys and class inheritances set up.</p> <p>*Note: you example defines type_id as Numeric, however the values seem to be Strings.*</p> <pre><code>Base = declarative_base(cls=_BaseMixin) Base.query = session.query_property() class BaseModel(Base): __tablename__ = 'entities' id = Column(Integer, primary_key=True) #type_id = Column(Integer, nullable=False) type_id = Column(String, nullable=False) __mapper_args__ = { 'polymorphic_identity': 'entity', 'polymorphic_on':type_id, 'with_polymorphic':'*' } class ModelB(BaseModel): __tablename__ = 'modelB' __mapper_args__ = {'polymorphic_identity': 'modelB'} id = Column(Integer, ForeignKey('entities.id'), primary_key=True) name = Column(String, nullable=False) class ModelC(BaseModel): __tablename__ = 'modelC' __mapper_args__ = {'polymorphic_identity': 'modelC'} id = Column(Integer, ForeignKey('entities.id'), primary_key=True) name = Column(String, nullable=False) class ModelD(BaseModel): __tablename__ = 'modelD' __mapper_args__ = {'polymorphic_identity': 'modelD'} id = Column(Integer, ForeignKey('entities.id'), primary_key=True) name = Column(String, nullable=False) class ModelE(ModelD): __tablename__ = 'modelE' __mapper_args__ = {'polymorphic_identity': 'modelE'} id = Column(Integer, ForeignKey('entities.id'), ForeignKey('modelD.id'), primary_key=True) name = Column(String, nullable=False) class ModelF(ModelD): __tablename__ = 'modelF' __mapper_args__ = {'polymorphic_identity': 'modelF'} id = Column(Integer, ForeignKey('entities.id'), ForeignKey('modelD.id'), primary_key=True) name = Column(String, nullable=False) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload