neutron源码学习基础知识储备之Sqlalchemy库
本文主要参考官方文档和一些网上资料,并结合之前python-web-frame项目使用来详细说明Sqlalchemy的使用,版本号为SQLAlchemy 1.1。
Sqlalchemy的架构?
Object Relational Mapper && SQL Expression Language ?
下面是截取python-web-frame项目代码
|
|
Connecting
|
|
echo flag is a shortcut to setting up SQLAlchemy logging, which is accomplished via Python’s standard logging module. With it enabled, we’ll see all the generated SQL produced.
echo意思说开启日志,你可以看到整个SQL是如何产生的,方便调试。
_ENGINE is an instance of Engine, and it represents the core interface to the database, adapted through a dialect that handles the details of the database and DBAPI in use.
_ENGINE 意思说与数据库打交道的核心接口
Declare a Mapping
|
|
生成一个映射使用的Base.
Create a Schema
|
|
db_User类继承了Base类,它具有metadata属性,通过create_all()方法,注入与数据库打交道的核心接口_ENGINE,我们发现有一系列的命令完成数据库中user表是否存在的检测和生成。
Creating a Session
|
|
This custom-made Session class will create new Session objects which are bound to our database.
Querying
http://docs.sqlalchemy.org/en/rel_1_1/orm/query.html#sqlalchemy.orm.query.Query
query()和 aliased()
Common Filter Operators
filter()
|
|
Returning Lists and Scalars
all() returns a list
first() applies a limit of one and returns the first result as a scalar
one() fully fetches all rows, and if not exactly one object identity or composite row is present in the result, raises an error
注意:The one() method is great for systems that expect to handle “no items found” versus “multiple items found” differently; such as a RESTful web service, which may want to raise a “404 not found” when no results are found, but raise an application error when multiple results are found.
one_or_none() is like one(), except that if no results are found, it doesn’t raise an error; it just returns None. Like one(), however, it does raise an error if multiple results are found
scalar() invokes the one() method, and upon success returns the first column of the row
Using Textual SQL
text()
Counting
count()
Building a Relationship
一对多
|
|
即:一个db_user对应多个db_Telephone
一对一
|
|
多对多
Many to Many adds an association table between two classes.
多对多关系会在两个类之间增加一个关联的表。
The association table is indicated by the secondary argument to relationship().
这个关联的表在 relationship() 方法中通过 secondary 参数来表示。
Usually, the Table uses the MetaData object associated with the declarative base class,
通常的,这个表会通过 MetaData 对象来与声明基类关联,
so that the ForeignKey directives can locate the remote tables with which to link:
所以这个 ForeignKey 指令会使用链接来定位到远程的表:
|
|
Querying with Joins
Using Aliases
Using EXISTS
Common Relationship Operators
eq() (many-to-one “equals” comparison)
ne() (many-to-one “not equals” comparison)
IS NULL (many-to-one comparison, also uses eq())
contains() (used for one-to-many collections)
any() (used for collections)
has() (used for scalar references)
Query.with_parent() (used for any relationship)
Eager Loading
Query.options()
subqueryload()第一种
Joined Load()第二种
contains_eager()第三种
Deleting
db_user与db_Telephone是一对多关系,下面操作解决了删除db_user,会自动删除关联的表数据
|
|
参考:
http://docs.sqlalchemy.org/en/rel_1_0/orm/tutorial.html
http://docs.sqlalchemy.org/en/rel_1_0/core/tutorial.html
http://blog.csdn.net/zd0303/article/details/50261347
http://blog.csdn.net/Jmilk/article/details/52445093#one-to-many