没有合适的资源?快使用搜索试试~ 我知道了~
首页python sqlalchemy 使用手册 英文版
python sqlalchemy 使用手册 英文版
4星 · 超过85%的资源 需积分: 9 11 下载量 22 浏览量
更新于2023-03-16
评论
收藏 1.92MB PDF 举报
python sqlalchemy tutorial english 使用手册 英文版
资源详情
资源评论
资源推荐
SQLAlchemy Documentation
Release 0.7.10
Mike Bayer
November 19, 2015
Contents
1 Overview 3
1.1 Documentation Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Code Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Installation Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.1 Supported Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 Supported Installation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.3 Install via easy_install or pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.4 Installing using setup.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.5 Installing the C Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.6 Installing on Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.7 Installing a Database API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.8 Checking the Installed SQLAlchemy Version . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 0.6 to 0.7 Migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 SQLAlchemy ORM 7
2.1 Object Relational Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 Version Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.2 Connecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.3 Declare a Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.4 Create an Instance of the Mapped Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.1.5 Creating a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.6 Adding New Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.7 Rolling Back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.8 Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Common Filter Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Returning Lists and Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Using Literal SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.1.9 Building a Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.1.10 Working with Related Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.1.11 Querying with Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Using Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Selecting Entities from Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using EXISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Common Relationship Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.1.12 Eager Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Subquery Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
i
Joined Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Explicit Join + Eagerload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.1.13 Deleting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Configuring delete/delete-orphan Cascade . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.1.14 Building a Many To Many Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.1.15 Further Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.2 Mapper Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.2.1 Classical Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.2.2 Customizing Column Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Naming Columns Distinctly from Attribute Names . . . . . . . . . . . . . . . . . . . . . . . 39
Naming All Columns with a Prefix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Using column_property for column level options . . . . . . . . . . . . . . . . . . . . . . . . 40
Mapping a Subset of Table Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.2.3 Deferred Column Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Column Deferral API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.2.4 SQL Expressions as Mapped Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Using a Hybrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Using column_property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Using a plain descriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.2.5 Changing Attribute Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Simple Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Using Descriptors and Hybrids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Custom Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.2.6 Composite Column Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Tracking In-Place Mutations on Composites . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Redefining Comparison Operations for Composites . . . . . . . . . . . . . . . . . . . . . . . 55
2.2.7 Mapping a Class against Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.2.8 Mapping a Class against Arbitrary Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.2.9 Multiple Mappers for One Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.2.10 Constructors and Object Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.2.11 Class Mapping API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.3 Relationship Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.3.1 Basic Relational Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
One To Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Many To One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
One To One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Many To Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Association Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.3.2 Adjacency List Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Self-Referential Query Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Configuring Self-Referential Eager Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2.3.3 Linking Relationships with Backref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Backref Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
One Way Backrefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
2.3.4 Setting the primaryjoin and secondaryjoin . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Specifying Alternate Join Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Self-Referential Many-to-Many Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Specifying Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Building Query-Enabled Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
2.3.5 Rows that point to themselves / Mutually Dependent Rows . . . . . . . . . . . . . . . . . . 85
2.3.6 Mutable Primary Keys / Update Cascades . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
2.3.7 Relationships API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
2.4 Collection Configuration and Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
ii
2.4.1 Working with Large Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Dynamic Relationship Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Setting Noload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Using Passive Deletes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
2.4.2 Customizing Collection Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Dictionary Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
2.4.3 Custom Collection Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Annotating Custom Collections via Decorators . . . . . . . . . . . . . . . . . . . . . . . . . 100
Custom Dictionary-Based Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Instrumentation and Custom Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
2.4.4 Collection Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
2.5 Mapping Class Inheritance Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
2.5.1 Joined Table Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Basic Control of Which Tables are Queried . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Advanced Control of Which Tables are Queried . . . . . . . . . . . . . . . . . . . . . . . . . 110
Creating Joins to Specific Subtypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
2.5.2 Single Table Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
2.5.3 Concrete Table Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Concrete Inheritance with Declarative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2.5.4 Using Relationships with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Relationships with Concrete Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2.5.5 Using Inheritance with Declarative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
2.6 Using the Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
2.6.1 What does the Session do ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
2.6.2 Getting a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Adding Additional Configuration to an Existing sessionmaker() . . . . . . . . . . . . . . . . 117
Creating Ad-Hoc Session Objects with Alternate Arguments . . . . . . . . . . . . . . . . . . 118
2.6.3 Using the Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Quickie Intro to Object States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Session Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Adding New or Existing Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Merging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Deleting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Flushing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Committing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Rolling Back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Expunging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Closing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Refreshing / Expiring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Session Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
2.6.4 Cascades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Controlling Cascade on Backrefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
2.6.5 Managing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Using SAVEPOINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Autocommit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Enabling Two-Phase Commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.6.6 Embedding SQL Insert/Update Expressions into a Flush . . . . . . . . . . . . . . . . . . . 134
2.6.7 Using SQL Expressions with Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
2.6.8 Joining a Session into an External Transaction . . . . . . . . . . . . . . . . . . . . . . . . . 135
2.6.9 Contextual/Thread-local Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Implicit Method Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Thread-Local Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Using Thread-Local Scope with Web Applications . . . . . . . . . . . . . . . . . . . . . . . 138
iii
剩余574页未读,继续阅读
ennismar
- 粉丝: 9
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1