python - SQLAlchemy - 映射器配置和声明性基础

标签 python sqlalchemy

我正在编写一个多媒体存档数据库后端,我想使用连接表继承。我将 Python 与带有声明性扩展的 SQLAlchemy 一起使用。保存媒体记录的表如下:

_Base = declarative_base()

class Record(_Base):
    __tablename__ = 'records'

    item_id = Column(String(M_ITEM_ID), ForeignKey('items.id'))
    storage_id = Column(String(M_STORAGE_ID), ForeignKey('storages.id'))
    id = Column(String(M_RECORD_ID), primary_key=True)
    uri = Column(String(M_RECORD_URI))
    type = Column(String(M_RECORD_TYPE))
    name = Column(String(M_RECORD_NAME))

type 列是鉴别符。现在我想从 Record 类定义子类 AudioRecord,但我不知道如何使用声明性语法。我正在寻找以下代码的等效项(来自 SQLAlchemy 文档):

mapper(Record, records, polymorphic_on=records.c.type, polymorphic_identity='record')
mapper(AudioRecord, audiorecords, inherits=Record, polymorphic_identity='audio_record')

如何将 polymorphic_onpolymorphic_identityinherits 关键字传递给声明式扩展创建的映射器?

谢谢 简

最佳答案

关于python - SQLAlchemy - 映射器配置和声明性基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/792588/

相关文章:

python - ItemIsAutoTristate 标志未按预期工作

python - 如何在 python 中计算 NTLM 哈希?

stored-procedures - SQLAlchemy 从 postgresql 存储过程获取输出参数

python - 如何在 Google AppEngine 上以字节为单位获取 python 对象的大小?

python - 谷歌关闭中的文件上传功能?

python - 我在无限循环的尝试中做错了什么?

python - 如何将 sql "dump"文件加载到 sql alchemy 中

python - 创建 alembic 迁移以创建 version_table_schema 的架构

python - 如何从 sqlalchemy 映射创建表

python - sqlalchemy.exc.InvalidRequestError : One or more mappers failed to initialize - can't proceed with initialization of other mappers 错误