理解SQLAlchemy的表继承关系(2)-Single Table Inheritance
来源:互联网 发布:学院风服装 知乎 编辑:程序博客网 时间:2024/05/17 04:38
Single Table Inheritance即单表继承,顾名思义,所有继承表的数据均保存在一个表。
该种继承比较容易理解。
class Employee(Base): __tablename__ = 'employee' id = Column(Integer, primary_key=True name = Column(String(50)) manager_data = Column(String(50)) engineer_info = Column(String(50)) type = Column(String(20)) __mapper_args__ = { 'polymorphic_on':type, 'polymorphic_identity':'employee' }class Manager(Employee): __mapper_args__ = { 'polymorphic_identity':'manager'}class Engineer(Employee): __mapper_args__ = { 'polymorphic_identity':'engineer' }
在上例中,Engineer和Manager均没有独立的表,所有数据均保存在基表Employee中,
同样的Employee使用polymorphic_on指定的字段来标识该行记录是属于哪个继承表的,而标识的字段串由继承表的
polymorphic_identity值指定。
在上例中polymorphic_on="type",Engineer的polymorphic_identity="engineer",
因此当你使用
Session.query(Engineer).all()
查询所有工程师时,相当于:
select * from employee where type="engineer"
小结:
1、所有继承表数据均在一个表中
2、基表的设计上应包含所有继承表的字段。
3、通过polymorphic_on来标识区分该记录属于哪个继承表.
0 0
- 理解SQLAlchemy的表继承关系(2)-Single Table Inheritance
- 理解SQLAlchemy的表继承关系(1)--Joined Table Inheritance
- 理解SQLAlchemy的表继承关系(3)-Concrete Table Inheritance
- single-table inheritance 单表继承
- 理解SQLAlchemy的表继承关系(4)--高级应用
- 单继承(single inheritance)
- [ROR]Single Table Inheritance with Rails 4 (Part 2)
- 类之间的关系(3. 继承(Inheritance)关系-2)
- hibernate实现继承关系Inheritance
- java的继承 inheritance
- sqlalchemy一对多的关系表案例
- Java的Inheritance继承-笔记(面向对象2-继承)
- [ROR]Single Table Inheritance with Rails 4 (Part 1)
- [ROR]Single Table Inheritance with Rails 4 (Part 3)
- 类之间的关系(2. 继承(Inheritance)关系-1)
- C++(12):单继承(single inheritance)
- c++类的继承(inheritance)
- Cpp的继承Inheritance-笔记
- MySQL锁定状态查看命令
- 网易2016研发工程师笔试题
- CentOS 6.5 系统 LAMP(Apache+MySQL+PHP+nginx+memcache) Yum安装步骤
- 黑马hadoop视频分享
- Ubuntu常用指令与快捷键大全,整理中...
- 理解SQLAlchemy的表继承关系(2)-Single Table Inheritance
- 广播地址
- 单元测试的艺术
- 开源监控软件之争
- 51nod 1058 N的阶乘的长度
- Java 性能优化系列之3.1[JVM调优]
- 关于form表单button事件教训
- Esper企业版介绍(二)Esper HQ / WEB UI
- android:ToolBar详解(手把手教程)