sqlalchemy:与AbstractEntities一对一关系

时间:2018-08-02 16:07:07

标签: python orm sqlalchemy

我在AbstractEntities中有一个一对一关系的探针,我有3个类,并且我将从状态对象访问Field的实例:

  • 国家等级
class State(Base):
    __tablename__ = 'states'

    id = Column('state_id', Integer, autoincrement=True, primary_key=True)
    start = Column('state_start', Date)
    end = Column('state_end', Date)

    field = relationship('Field', uselist=False, back_populates='state')
  • 摘要字段
class Field(AbstractConcreteBase, Base):
    @declared_attr
    def state_id(cls):
        return Column(Integer, ForeignKey('states.state_id'))

    @declared_attr
    def state(cls):
        return relationship('State', back_populates='field')
  • 2个子类FieldName,FieldAge
class FieldName(Field):
    __tablename__ = 'fields_name'
    __mapper_args__ = {
        'polymorphic_identity': 'fields_name',
        'concrete': True
    }

    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column('name', String(10))


class FieldAge(Field):
    __tablename__ = 'fields_age'
    __mapper_args__ = {
        'polymorphic_identity': 'fields_age',
        'concrete': True
    }

    id = Column(Integer, autoincrement=True, primary_key=True)
    age = Column('age', Integer)

但是当我尝试使用Base.metadata.create_all(engine)创建架构时,sqlalchemy会抛出此错误:

 sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|FieldAge|fields_age'. Original exception was: reverse_property 'field' on relationship FieldAge.state references relationship State.field, which does not reference mapper Mapper|FieldAge|fields_age
 venv\lib\site-packages\sqlalchemy\orm\mapper.py:3026: InvalidRequestError

感谢您的帮助,

最诚挚的问候,

0 个答案:

没有答案