SQLAlchemy抽象类关系

时间:2017-05-11 06:16:41

标签: python sqlalchemy

在我的应用中,我想制作以下层次结构:

class BaseCadastralObject(Base):
    abstract__ = True

    cadastral_region = Column(Integer, primary_key=True, index=True)
    cadastral_district = Column(Integer, primary_key=True, index=True)
    cadastral_object = Column(Integer, primary_key=True, index=True)

这些字段组合了一个主键。

A有一个子课程,例如:

class Building(BaseCadastralObject):
    __tablename__ == 'buildings'
    ...
class Construction(BaseCadastralObject):
    __tablename__ == 'constructions'
    ...

我还有一个表应该与这样的对象(多对多)相关:

class Parameter(Base):

    __tablename__ == 'parameters'
    parent_cadastral_region = Column(Integer, nullable=False)
    parent_cadastral_district = Column(Integer, nullable=False)
    parent_cadastral_object = Column(Integer, nullable=False)

    parent_building = relationship(BaseCadastralObject, backref='parameters')
                                   ^^^^^^^^^^^^^^^^^^^
    __table_args__ = (ForeignKeyConstraint(
        [parent_cadastral_region, parent_cadastral_district, parent_cadastral_object],
        [BaseCadastralObject.cadastral_region, BaseCadastralObject.cadastral_district, BaseCadastralObject.cadastral_object]),
                  {}
)

我应该能够做到:

b = Building(...)
parameters = b.parameters

但是我得到了不同的错误,比如这个:

sqlalchemy.orm.exc.UnmappedClassError: Class 'app.models.BaseCadastralObject' is not mapped

0 个答案:

没有答案