关于python:SQLAlchemy – Mapper配置和声明基础

SQLAlchemy - Mapper configuration and declarative base

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

1
2
3
4
5
6
7
8
9
10
11
_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,但我不想使用声明性语法设置多态映射器。我正在寻找以下代码的等效代码(来自sqlacalchemy文档):

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

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

谢谢你简


我终于在手册中找到了答案。

http://www.sqlachemy.org/docs/05/reference/ext/declarative.html联接表继承