python - 类不以多态方式加载

标签 python postgresql sqlalchemy

使用 SQLAlchemy,我已经为单表继承配置了类。当我查询基类时,没有加载特定的类。相反,只加载基类的实例。我配置错误了吗?

from sqlalchemy import Column, Integer, Enum
from sqlalchemy.dialects.postgresql import TEXT

from sqlalchemy import MetaData, create_engine
from sqlalchemy.orm import sessionmaker, scoped_session

from sqlalchemy.ext.declarative import declarative_base

info = 'postgresql+psycopg2://panel_app@localhost:5432/panel_db'

engine = create_engine(info)

metadata = MetaData()
session = scoped_session(sessionmaker(bind=engine))

Base = declarative_base(metadata=metadata)

class Wave(Base):
    """Represent a Wave."""

    __tablename__ = 'waves'

    wave_id = Column(Integer, primary_key=True)
    wave_name = Column(TEXT)
    type = Column(Enum('emailed', 'triggered', 'anonymous', name='wave_type'), nullable=False)

    __mapper_args = {
        'polymorphic_on': type,
    }


class AnonymousWave(Wave):

    __mapper_args__ = {
        'polymorphic_identity': 'anonymous'
    }


class EmailedWave(Wave):
    """Represents a wave that is emailed out."""

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

数据库中的行是

select wave_id, wave_name, type from waves;
 wave_id |            wave_name             |  type
---------+----------------------------------+---------
       1 | Wave 1                           | emailed
      10 | MM 1 - client approval test      | emailed
       2 | Wave 1                           | emailed
       3 | Wave 1                           | emailed
       4 | Wave 1                           | emailed
       5 | Wave 1                           | emailed
       6 | Wave 1                           | emailed
       7 | Wave 1                           | emailed
       8 | Wave 1                           | emailed
(9 rows)

示例代码:

waves = session.query(Wave).all()
from pprint import pprint
pprint(waves)
for wave in waves:
    print wave.__class__.__name__ + " " + wave.type

及其输出:

[<__main__.Wave object at 0x2324b50>,
 <__main__.Wave object at 0x2324d50>,
 <__main__.Wave object at 0x2324dd0>,
 <__main__.Wave object at 0x2324e50>,
 <__main__.Wave object at 0x2324ed0>,
 <__main__.Wave object at 0x2324f90>,
 <__main__.Wave object at 0x2329090>,
 <__main__.Wave object at 0x2329150>,
 <__main__.Wave object at 0x2329210>]
Wave emailed
Wave emailed
Wave emailed
Wave emailed
Wave emailed
Wave emailed
Wave emailed
Wave emailed
Wave emailed

最佳答案

__mapper_args = {
    'polymorphic_on': type,
}

是你的问题。

这是一个很难看到的错字,但是你漏掉了两个下划线 '__'

__mapper_args__ = {
    'polymorphic_on': type,
}

关于python - 类不以多态方式加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709073/

相关文章:

python - 使父对象不出现在 before_flush 事件监听器的 session.dirty 中

r - RPostgres 中是否有一个函数可以为 Postgres 中的表创建/标识主键?

sql - 如何在 Rails 的最后一个给定字段中排序并返回具有给定值的记录?

python - 现有实例上的 SQLAlchemy 类继承

python - 在 bash 中循环遍历数组

postgresql - 如何跨两个表管理唯一列

python - 为 View 可调用项设置 sqlalchemy 连接的推荐方法是什么?

Python3 - 如何将字符串转换为十六进制

Python进程在执行过程中突然终止

python - 如何从 jupyter 中提取整个可执行文件并清除脚本