映射类继承层次结构映射失败

时间:2014-08-27 15:58:10

标签: inheritance sqlalchemy hierarchy

我有以下代码:

class Entity(Base):
    __tablename__ = 'entity'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    entity_type = Column('type',Enum('asset','institution','model'))
    source_table_id = Column(Integer)

    __mapper_args__ = {
        'polymorphic_identity':'entity',
        'polymorphic_on':entity_type,
        }

class Asset(Entity):    
    __tablename__ = 'asset'

    id = Column('entity_id',Integer, ForeignKey('entity.id'), primary_key=True)
    asset_type = Column('type',Enum('listed','private'))

    __mapper_args__ = {
        'polymorphic_identity':'asset',
        'polymorphic_on':asset_type,
        'with_polymorphic':'*'
        }

class Listed(Asset):
    __tablename__ = 'listed'

    id = Column('asset_entity_id',Integer, ForeignKey('asset.entity_id'), primary_key=True)
    ticker = Column('ticker',String)
    isin = Column('isin',String)

    __mapper_args__ = {
        'polymorphic_identity':'listed',
        }

我相信如果我查询Entity,我应该找回Listed个对象。

工作顺序

  1. 清除工作区
  2. Listed执行查询会产生Listed个对象。
  3. Entity执行查询会产生Listed个对象。
  4. 序列破碎

    1. 清除工作区
    2. Entity执行查询会产生Asset个对象。
    3. Listed执行查询会产生Asset个对象。
    4. 控制台

      In [75]: reset
      
      Once deleted, variables cannot be recovered. Proceed (y/[n])? y
      
      
      In [76]: from fms import *
      
      In [77]: import test
      
      In [78]: listeds = session.query(test.Listed).filter(test.Listed.id == 2605).all()
          ...: l = listeds[0]
          ...: l.isin
      Out[78]: 'ZAE000115184'
      
      In [79]: entities = session.query(test.Entity).filter(test.Entity.id == 2605).all()
          ...: e = entities[0]
          ...: e.isin
      Out[79]: 'ZAE000115184'
      
      In [80]: reset
      
      Once deleted, variables cannot be recovered. Proceed (y/[n])? y
      
      In [81]: from fms import *
      
      In [82]: import test
      
      In [83]: entities = session.query(test.Entity).filter(test.Entity.id == 2605).all()
          ...: e = entities[0]
          ...: e.isin
      ---------------------------------------------------------------------------
      AttributeError                            Traceback (most recent call last)
      <ipython-input-83-78a0841e0935> in <module>()
            1 entities = session.query(test.Entity).filter(test.Entity.id == 2605).all()
            2 e = entities[0]
      ----> 3 e.isin
      
      AttributeError: 'Asset' object has no attribute 'isin'
      
      In [84]: listeds = session.query(test.Listed).filter(test.Listed.id == 2605).all()
          ...: l = listeds[0]
          ...: l.isin
      ---------------------------------------------------------------------------
      AttributeError                            Traceback (most recent call last)
      <ipython-input-84-537bcd7f1cb3> in <module>()
            1 listeds = session.query(test.Listed).filter(test.Listed.id == 2605).all()
            2 l = listeds[0]
      ----> 3 l.isin
      
      AttributeError: 'Asset' object has no attribute 'isin'
      

0 个答案:

没有答案