3
我有一個類映射兩個多態繼承:SQLAlchemy的 - 雙多態繼承問題
#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
我堅持一個ClassA的對象:
object = ClassA('toto')
db_session.add(object)
db_session.commit()
當我嘗試重新加載這樣的對象:
reloaded_object = AbstractB.query.first()
我得到一個ClassA的對象(就好了)
但是當我嘗試重新加載這樣的:
reloaded_object = AbstractA.query.first()
我得到一個AbstractA對象,因爲abstract_a.class_name尚未設置爲polymorphic_identity。
這是問題還是預期的工作?