SQLAlchemy 1.1文档
Selectables,Tables,FROM objects ¶
The term “selectable” refers to any object that rows can be selected from; in SQLAlchemy, these objects descend from FromClause
and their distinguishing feature is their FromClause.c
attribute, which is a namespace of all the columns contained within the FROM clause (these elements are themselves ColumnElement
subclasses).
-
sqlalchemy.sql.expression.
alias
(selectable, name=None, flat=False)¶ 返回一个
Alias
对象。An
Alias
represents anyFromClause
with an alternate name assigned within SQL, typically using theAS
clause when generated, e.g.SELECT * FROM table AS aliasname
.类似的功能可以通过所有
FromClause
子类上的alias()
方法使用。当从
Table
对象创建Alias
时,这会使表格呈现为tablename AS < / t8> aliasname
。For
select()
objects, the effect is that of creating a named subquery, i.e.(select ...) AS aliasname
.name
参数是可选的,并提供在呈现的SQL中使用的名称。如果为空,则会在编译时确定性地生成“匿名”名称。确定性意味着该名称对于在同一语句中使用的其他构造是唯一的,并且对于同一个语句对象的每个连续编译也将是相同的名称。参数: - selectable¶ – any
FromClause
subclass, such as a table, select statement, etc. - name¶ – string name to be assigned as the alias. 如果
None
,则会在编译时确定性地生成名称。 - flat ¶ -
如果给定的selectable是
Join
的实例,请参阅Join.alias()
以获取详细信息。版本0.9.0中的新功能
- selectable¶ – any
-
sqlalchemy.sql.expression.
except_
(*selects, **kwargs)¶ 返回多个可选项的
EXCEPT
。返回的对象是
CompoundSelect
的一个实例。
-
sqlalchemy.sql.expression.
except_all
(*selects, **kwargs)¶ 返回多个可选项的
EXCEPT ALL
。返回的对象是
CompoundSelect
的一个实例。
-
sqlalchemy.sql.expression.
exists
(*args, **kwargs)¶ 针对现有的
Select
对象构建新的Exists
。调用样式具有以下形式:
# use on an existing select() s = select([table.c.col1]).where(table.c.col2==5) s = exists(s) # construct a select() at once exists(['*'], **select_arguments).where(criterion) # columns argument is optional, generates "EXISTS (SELECT *)" # by default. exists().where(table.c.col2==5)
-
sqlalchemy.sql.expression.
intersect
(*selects, **kwargs)¶ Return an
INTERSECT
of multiple selectables.返回的对象是
CompoundSelect
的一个实例。
-
sqlalchemy.sql.expression.
intersect_all
(*selects, **kwargs)¶ 返回多个可选项的
INTERSECT ALL
。返回的对象是
CompoundSelect
的一个实例。
-
sqlalchemy.sql.expression.
join
(left, right, onclause=None, isouter=False, full=False)¶ 给定两个
FromClause
表达式,产生一个Join
对象。例如。:
j = join(user_table, address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
Similar functionality is available given any
FromClause
object (e.g. such as aTable
) using theFromClause.join()
method.参数: - left ¶ - 连接的左侧。
- 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是JOIN。
版本1.1中的新功能
-
sqlalchemy.sql.expression.
lateral
(selectable, name=None)¶ 返回一个
Lateral
对象。Lateral
是一个Alias
子类,它表示应用了LATERAL关键字的子查询。LATERAL子查询的特殊行为是它出现在封闭SELECT的FROM子句中,但可能与该SELECT的其他FROM子句相关。这是子查询的一种特殊情况,只有少数后端支持,现在是更新的Postgresql版本。
版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
-
sqlalchemy.sql.expression.
outerjoin
(left, right, onclause=None, full=False)¶ 返回一个
OUTER JOIN
子句元素。返回的对象是
Join
的实例。类似的功能也可以通过任何
FromClause
上的outerjoin()
方法使用。参数: 要将连接链接在一起,请在生成的
Join
对象上使用FromClause.join()
或FromClause.outerjoin()
方法。
-
sqlalchemy.sql.expression.
select
(columns=None, whereclause=None, from_obj=None, distinct=False, having=None, correlate=True, prefixes=None, suffixes=None, **kwargs)¶ 构建一个新的
Select
。通过任何
FromClause
上的FromClause.select()
方法也可以获得类似的功能。接受
ClauseElement
参数的所有参数也接受字符串参数,这些参数将根据需要转换为text()
或literal_column()
结构。参数: - 列 ¶ -
ColumnElement
或FromClause
对象的列表,这些对象将形成结果语句的列子句。For those objects that are instances ofFromClause
(typicallyTable
orAlias
objects), theFromClause.c
collection is extracted to form a collection ofColumnElement
objects.该参数还将接受给定的
Text
结构,以及ORM映射的类。注意
select.columns
参数在select()
的方法形式中不可用,例如,FromClause.select()
- whereclause ¶ -
将用于形成
WHERE
子句的ClauseElement
表达式。通常最好将WHERE标准添加到与Select.where()
链接的现有Select
使用方法中。也可以看看
- from_obj ¶ -
将被添加到结果语句的
FROM
子句中的ClauseElement
对象的列表。这相当于在现有的Select
对象上使用方法链接调用Select.select_from()
。也可以看看
Select.select_from()
- full description of explicit FROM clause specification. - autocommit ¶ -
已过时。使用
.execution_options(autocommit=<True|False>)
来设置自动提交选项。 - bind=None¶ – an
Engine
orConnection
instance to which the resultingSelect
object will be bound. 否则Select
对象将自动绑定到Connectable
实例可以位于其包含的ClauseElement
成员中的任何位置。 - correlate = True ¶ -
表明这个
Select
对象应该包含FromClause
元素与一个封闭的Select
对象“相关”。通常最好使用Select.correlate()
指定现有Select
构造上的相关性。也可以看看
Select.correlate()
- full description of correlation. - distinct = False ¶ -
当
True
时,将DISTINCT
限定符应用于结果语句的columns子句。布尔参数也可以是列表达式或列表达式的列表 - 这是一种特殊的调用形式,Postgresql方言可以理解为
DISTINCT ON (&lt; columns&gt;)
语法。通过
distinct()
方法,现有的Select
对象也可以使用distinct
。也可以看看
- for_update = False ¶ - 当
True
时,将FOR UPDATE
应用到结果语句的末尾。Deprecated since version 0.9.0: - use
Select.with_for_update()
to specify the structure of theFOR UPDATE
clause.for_update
接受由特定后端解释的各种字符串值,包括:"read"
- on MySQL, translates toLOCK IN SHARE MODE
; on Postgresql, translates toFOR SHARE
."nowait"
- on Postgresql and Oracle, translates toFOR UPDATE NOWAIT
."read_nowait"
- on Postgresql, translates toFOR SHARE NOWAIT
.
也可以看看
Select.with_for_update()
- improved API for specifying theFOR UPDATE
clause. - group_by ¶ -
将包含结果选择的
GROUP BY
子句的ClauseElement
对象的列表。This parameter is typically specified more naturally using theSelect.group_by()
method on an existingSelect
.也可以看看
- 有 ¶ -
当
GROUP BY
时,ClauseElement
将包含结果选择的HAVING
用来。此参数通常在现有Select
中使用Select.having()
方法更自然地指定。也可以看看
- 限制=无 ¶ -
一个数值,通常在结果选择中呈现为
LIMIT
表达式。不支持LIMIT
的后端将尝试提供类似的功能。此参数通常在现有的Select
中使用Select.limit()
方法来更自然地指定。也可以看看
- 偏移量=无 ¶ -
a numeric value which usually renders as an
OFFSET
expression in the resulting select. 不支持OFFSET
的后端将尝试提供类似的功能。此参数通常在现有Select
中使用Select.offset()
方法更自然地指定。也可以看看
- order_by ¶ -
一个标量或
ClauseElement
对象的列表,它们将包含结果select的ORDER BY
子句。此参数通常在现有的Select
中使用Select.order_by()
方法更自然地指定。也可以看看
- use_labels = False ¶ -
当
True
时,将使用columns子句中每列的标签生成该语句,该列使用其父表的名称(或别名)限定每个列,以便不同表中的列之间的名称冲突不会发生。标签的格式是_ 生成的。 T1> T0> Select
对象的“c”集合也将使用这些名称来定位列成员。也可以使用
Select.apply_labels()
方法在现有的Select
对象上指定此参数。
- 列 ¶ -
sqlalchemy.sql.expression。
子查询 tt> ( 别名,* args, ** kwargs T5> ) T6> ¶ T7>
- 名称
- 别名
* args,** kwargs
所有其他参数传递给select()
函数。
-
sqlalchemy.sql.expression.
table
(name, *columns)¶ 产生一个新的
TableClause
。返回的对象是一个
TableClause
实例,它表示模式级别Table
对象的“语法”部分。它可能被用来构造轻量级的表格结构。Changed in version 1.0.0:
expression.table()
can now be imported from the plainsqlalchemy
namespace like any other SQL element.参数: - 名称 ¶ - 表格的名称。
- columns¶ – A collection of
expression.column()
constructs.
-
sqlalchemy.sql.expression.
tablesample
(selectable, sampling, name=None, seed=None)¶ 返回一个
TableSample
对象。TableSample
is anAlias
subclass that represents a table with the TABLESAMPLE clause applied to it.tablesample()
is also available from theFromClause
class via theFromClause.tablesample()
method.TABLESAMPLE子句允许从表中选择一个随机选择的近似百分比的行。它支持多种采样方法,最常见的是BERNOULLI和SYSTEM。
例如。:
from sqlalchemy import func selectable = people.tablesample( func.bernoulli(1), name='alias', seed=func.random()) stmt = select([selectable.c.people_id])
假设
people
具有列people_id
,则上述语句将呈现为:SELECT alias.people_id FROM people AS alias TABLESAMPLE bernoulli(:bernoulli_1) REPEATABLE (random())
版本1.1中的新功能
参数: - sampling¶ – a
float
percentage between 0 and 100 orfunctions.Function
. - 名称 ¶ - 可选的别名
- seed¶ – any real-valued SQL expression. 指定时,也会呈现REPEATABLE子句。
- sampling¶ – a
-
sqlalchemy.sql.expression.
union
(*selects, **kwargs)¶ 返回多个可选择的
UNION
。返回的对象是
CompoundSelect
的一个实例。所有
FromClause
子类都有一个类似的union()
方法。
-
sqlalchemy.sql.expression.
union_all
(*selects, **kwargs)¶ 返回多个可选项的
UNION ALL
。返回的对象是
CompoundSelect
的一个实例。所有
FromClause
子类中都有一个类似的union_all()
方法。
- class
sqlalchemy.sql.expression。
别名
( 可选,名称=无 T5> ) T6> ¶ T7> 基础:
sqlalchemy.sql.expression.FromClause
表示一个表或可选别名(AS)。
使用
AS
关键字(或在某些数据库(例如Oracle)上没有关键字),通常应用于SQL语句内的任何表或子选择,表示别名。This object is constructed from the
alias()
module level function as well as theFromClause.alias()
method available on allFromClause
subclasses.-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
-
- class
sqlalchemy.sql.expression.
CompoundSelect
(keyword, *selects, **kwargs)¶ 基础:
sqlalchemy.sql.expression.GenerativeSelect
- Forms the basis of
UNION
,UNION ALL
, and other - 基于SELECT的集合操作。
-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
append_group_by T0> ( T1> *条款 T2> ) T3> ¶ T4>
追加应用于此可选项的给定GROUP BY标准。
该标准将被附加到任何预先存在的GROUP BY标准。
这是一种in-place突变方法;
group_by()
方法是首选,因为它提供了标准的method chaining。
append_order_by T0> ( T1> *条款 T2> ) T3> ¶ T4>
附加给定的ORDER BY标准应用于此可选项。
该标准将被附加到任何预先存在的ORDER BY标准。
这是一种in-place突变方法;
order_by()
方法是首选,因为它提供了标准的method chaining。
apply_labels T0> ( T1> ) T2> ¶ T3>
- 继承自
apply_labels()
方法GenerativeSelect
返回一个新的可选项,并将'use_labels'标志设置为True。
这将导致使用标签对其表名称生成列表达式,例如“SELECT somecolumn AS tablename_somecolumn”。这允许包含多个FROM子句的可选项生成一组唯一的列名称,而不考虑各个FROM子句之间的名称冲突。
as_scalar T0> ( T1> ) T2> ¶ T3>
- 继承自
as_scalar()
方法SelectBase
返回这个可选项的'标量'表示,它可以用作列表达式。
通常,在其子列中只有一列的select语句可以用作标量表达式。
返回的对象是
ScalarSelect
的一个实例。
自动提交 T0> ( T1> ) T2> ¶ T3>
- 继承自
autocommit()
方法SelectBase
将'autocommit'标志设置为True返回一个新的可选项。
从版本0.6开始弃用:
autocommit()
已弃用。使用带有'autocommit'标志的Executable.execution_options()
。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
-
cte
(name=None, recursive=False)¶ 返回一个新的
CTE
或公共表表达式实例。公用表表达式是一种SQL标准,通过使用一个名为“WITH”的子句,SELECT语句可以使用与主语句一起指定的次要语句。有关UNION的特殊语义也可用于允许“递归”查询,其中SELECT语句可以在先前已选择的一组行上进行绘制。
CTE也可以应用于DML构造对某些数据库的UPDATE,INSERT和DELETE,与RETURNING一起作为CTE行的来源以及CTE行的使用者。
SQLAlchemy将
CTE
对象检测为与Alias
对象类似的对象,作为要传递到语句的FROM子句的特殊元素以及顶部的WITH子句的声明。在版本1.1中进行了更改:添加了对CTE,CTE添加到UPDATE / INSERT / DELETE的UPDATE / INSERT / DELETE的支持。
参数: 以下示例包含两篇来自Postgresql的文档,位于http://www.postgresql.org/docs/current/static/queries-with.html以及其他示例。
例1,非递归:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select([ orders.c.region, func.sum(orders.c.amount).label('total_sales') ]).group_by(orders.c.region).cte("regional_sales") top_regions = select([regional_sales.c.region]).\ where( regional_sales.c.total_sales > select([ func.sum(regional_sales.c.total_sales)/10 ]) ).cte("top_regions") statement = select([ orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ]).where(orders.c.region.in_( select([top_regions.c.region]) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
例2,与RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select([ parts.c.sub_part, parts.c.part, parts.c.quantity]).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select([ parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ]). where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select([ included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ]).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
例3,使用带CTE的UPDATE和INSERT的upsert:
orders = table( 'orders', column('region'), column('amount'), column('product'), column('quantity') ) upsert = ( orders.update() .where(orders.c.region == 'Region1') .values(amount=1.0, product='Product1', quantity=1) .returning(*(orders.c._all_columns)).cte('upsert')) insert = orders.insert().from_select( orders.c.keys(), select([ literal('Region1'), literal(1.0), literal('Product1'), literal(1) ).where(exists(upsert.select())) ) connection.execute(insert)
也可以看看
orm.query.Query.cte()
-HasCTE.cte()
的ORM版本。
描述 T0> ¶ T1>
- 从
description
继承FromClause
这个FromClause的简要描述。
主要用于错误消息格式。
执行 tt> ( * multiparams,** params ) T5>
- 继承自
execute()
方法Executable
编译并执行
Executable
。
execution_options T0> ( T1> **千瓦 T2> ) T3> ¶ T4>
- 继承自
execution_options()
方法 tt>Executable
为执行期间生效的语句设置非SQL选项。
执行选项可以在每个语句或每个
Connection
的基础上设置。此外,Engine
和ORMQuery
对象提供对执行选项的访问,而这些执行选项在连接时进行配置。execution_options()
方法是生成的。返回此语句的新实例,其中包含以下选项:statement = select([table.c.x, table.c.y]) statement = statement.execution_options(autocommit=True)
请注意,只有一部分可能的执行选项可以应用于语句 - 这些选项包括“autocommit”和“stream_results”,但不包括“isolation_level”或“compiled_cache”。有关可能的选项的完整列表,请参阅
Connection.execution_options()
。
FOR_UPDATE T0> ¶ T1>
- 继承自
for_update
属性GenerativeSelect
为
for_update
属性提供传统方言支持。
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
GROUP_BY T0> ( T1> *条款 T2> ) T3> ¶ T4>
- 从
group_by()
方法继承的GenerativeSelect
返回一个新的可选项,其中应用了GROUP BY标准的给定列表。
该标准将被附加到任何预先存在的GROUP BY标准。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
标签 T0> ( T1> 名称 T2> ) T3> ¶ T4>
- 继承自
label()
方法SelectBase
返回这个可选择的“标量”表示,嵌入为带有标签的子查询。
也可以看看
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
限制 T0> ( T1> 限制 T2> ) T3> ¶ T4>
- inherited from the
limit()
method ofGenerativeSelect
返回一个新的可选择的给定LIMIT标准。
这是一个数值,通常在结果选择中呈现为
LIMIT
表达式。不支持LIMIT
的后端将尝试提供类似的功能。版本1.0.0更改: -
Select.limit()
现在可以接受任意SQL表达式以及整数值。参数: limit¶ – an integer LIMIT parameter, or a SQL expression that provides an integer result.
偏移 T0> ( T1> 偏移 T2> ) T3> ¶ T4>
- inherited from the
offset()
method ofGenerativeSelect
使用给定的OFFSET标准返回一个新的可选项。
This is a numeric value which usually renders as an
OFFSET
expression in the resulting select. 不支持OFFSET
的后端将尝试提供类似的功能。版本1.0.0更改: -
Select.offset()
现在可以接受任意SQL表达式以及整数值。参数: offset¶ – an integer OFFSET parameter, or a SQL expression that provides an integer result.
ORDER_BY T0> ( T1> *条款 T2> ) T3> ¶ T4>
- inherited from the
order_by()
method ofGenerativeSelect
返回一个新的可选项,其中应用了ORDER BY标准的给定列表。
该标准将被附加到任何预先存在的ORDER BY标准。
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
标量
( * multiparams,** params ) T5>- inherited from the
scalar()
method ofExecutable
编译并执行此
Executable
,返回结果的标量表示。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
-
with_for_update
(nowait=False, read=False, of=None, skip_locked=False, key_share=False)¶ - inherited from the
with_for_update()
method ofGenerativeSelect
为
GenerativeSelect
指定一个FOR UPDATE
子句。例如。:
stmt = select([table]).with_for_update(nowait=True)
在像Postgresql或Oracle这样的数据库上,上面的代码会显示如下的语句:
SELECT table.a, table.b FROM table FOR UPDATE NOWAIT
在其他后端,
nowait
选项被忽略,而是产生:SELECT table.a, table.b FROM table FOR UPDATE
当不带任何参数调用时,语句将以后缀
FOR UPDATE
进行呈现。然后可以提供其他参数,这些参数允许使用通用数据库特定的变体。参数: - nowait ¶ - boolean;将在Oracle和Postgresql方言中呈现
FOR tt> UPDATE NOWAIT
- 读 ¶ - boolean;将在MySQL上呈现
LOCK IN SHARE 模式
,在Postgresql上共享 共享
。On Postgresql, when combined withnowait
, will renderFOR SHARE NOWAIT
. - of¶ – SQL expression or list of SQL expression elements (typically
Column
objects or a compatible expression) which will render into aFOR UPDATE OF
clause; supported by PostgreSQL and Oracle. 可以根据后端呈现为表格或列。 - skip_locked ¶ -
boolean, will render
FOR UPDATE SKIP LOCKED
on Oracle and Postgresql dialects orFOR SHARE SKIP LOCKED
ifread=True
is also specified.版本1.1.0中的新功能
- key_share ¶ -
boolean, will render
FOR NO KEY UPDATE
, or if combined withread=True
will renderFOR KEY SHARE
, on the Postgresql dialect.版本1.1.0中的新功能
- nowait ¶ - boolean;将在Oracle和Postgresql方言中呈现
- Forms the basis of
- class
sqlalchemy.sql.expression.
CTE
(selectable, name=None, recursive=False, _cte_alias=None, _restates=frozenset([]), _suffixes=None)¶ 基础:
sqlalchemy.sql.expression.Generative
,sqlalchemy.sql.expression.HasSuffixes
,sqlalchemy.sql.expression.Alias
代表通用表达式。
CTE
对象是使用任何可选择的SelectBase.cte()
方法获得的。查看完整示例的方法。New in version 0.7.6.
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
后缀 tt> ( * expr,** kw ) T5>
- inherited from the
suffix_with()
method ofHasSuffixes
在整个语句后添加一个或多个表达式。
这用于在特定结构上支持后端特定的后缀关键字。
例如。:
stmt = select([col1, col2]).cte().suffix_with( "cycle empno set y_cycle to 1 default 0", dialect="oracle")
多个后缀可以通过多次调用
suffix_with()
来指定。参数: - *expr¶ – textual or
ClauseElement
construct which will be rendered following the target clause. - ** kw ¶ - 接受单个关键字'dialect'。这是一个可选的字符串方言名称,它将限制仅将该后缀渲染为该方言。
- *expr¶ – textual or
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
- class
sqlalchemy.sql.expression。
可执行文件
¶ 基础:
sqlalchemy.sql.expression.Generative
将一个ClauseElement标记为支持执行。
Executable
is a superclass for all “statement” types of objects, includingselect()
,delete()
,update()
,insert()
,text()
.结合 T0> ¶ T1>
返回此
Executable
绑定到的Engine
或Connection
;如果没有找到,则返回None。这是遍历,在本地进行检查,然后检查关联对象的“from”子句,直到找到绑定的引擎或连接。
执行 tt> ( * multiparams,** params ) T5>
编译并执行
Executable
。
execution_options T0> ( T1> **千瓦 T2> ) T3> ¶ T4>
为执行期间生效的语句设置非SQL选项。
执行选项可以在每个语句或每个
Connection
的基础上设置。此外,Engine
和ORMQuery
对象提供对执行选项的访问,而这些执行选项在连接时进行配置。execution_options()
方法是生成的。返回此语句的新实例,其中包含以下选项:statement = select([table.c.x, table.c.y]) statement = statement.execution_options(autocommit=True)
请注意,只有一部分可能的执行选项可以应用于语句 - 这些选项包括“autocommit”和“stream_results”,但不包括“isolation_level”或“compiled_cache”。有关可能的选项的完整列表,请参阅
Connection.execution_options()
。
标量
( * multiparams,** params ) T5>编译并执行此
Executable
,返回结果的标量表示。
- class
sqlalchemy.sql.expression。
FromClause
¶ 基础:
sqlalchemy.sql.expression.Selectable
表示可以在
SELECT
语句的FROM
子句中使用的元素。最常见的
FromClause
形式是Table
和select()
结构。所有FromClause
对象共有的主要特征包括:- 一个
c
集合,它提供对ColumnElement
对象集合的每个名称的访问。 - 一个
primary_key
属性,它是指示primary_key
标志的所有ColumnElement
对象的集合。 - Methods to generate various derivations of a “from” clause, including
FromClause.alias()
,FromClause.join()
,FromClause.select()
.
-
alias
(name=None, flat=False)¶ 返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
列 T0> ¶ T1>
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
对应列
( 列,require_embedded = False ) t5 >给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
描述 T0> ¶ T1>
这个FromClause的简要描述。
主要用于错误消息格式。
foreign_keys T0> ¶ T1>
返回FromClause引用的ForeignKey对象的集合。
is_derived_from T0> ( T1> fromclause T2> ) T3> ¶ T4>
如果FromClause从给定的FromClause中“派生”,则返回True。
一个例子是从表中派生的表的别名。
-
join
(right, onclause=None, isouter=False, full=False)¶ 从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
primary_key T0> ¶ T1>
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ 用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
架构
=无 ¶为
FromClause
定义'schema'属性。除
Table
以外的大多数对象,这通常是None
,它被视为Table.schema
参数的值。
选择
( whereclause = None,** params ) / T5>返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
-
tablesample
(sampling, name=None, seed=None)¶ 返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
- 一个
- class
sqlalchemy.sql.expression.
GenerativeSelect
(use_labels=False, for_update=False, limit=None, offset=None, order_by=None, group_by=None, bind=None, autocommit=None)¶ 基础:
sqlalchemy.sql.expression.SelectBase
可以添加其他元素的SELECT语句的基类。
这可以作为
Select
和CompoundSelect
的基础,其中可以添加ORDER BY,GROUP BY等元素,并且可以控制列呈现。与TextAsFrom
相比,它虽然它的子类为SelectBase
,并且也是一个SELECT构造,它表示一个固定的文本字符串,不能在此级别进行更改,只能打包为子查询。版本0.9.0新增:
GenerativeSelect
用于提供特定于Select
和CompoundSelect
的功能,同时允许SelectBase
用于其他类似SELECT的对象,例如TextAsFrom
-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
append_group_by T0> ( T1> *条款 T2> ) T3> ¶ T4>
追加应用于此可选项的给定GROUP BY标准。
该标准将被附加到任何预先存在的GROUP BY标准。
这是一种in-place突变方法;
group_by()
方法是首选,因为它提供了标准的method chaining。
append_order_by T0> ( T1> *条款 T2> ) T3> ¶ T4>
附加给定的ORDER BY标准应用于此可选项。
该标准将被附加到任何预先存在的ORDER BY标准。
这是一种in-place突变方法;
order_by()
方法是首选,因为它提供了标准的method chaining。
apply_labels T0> ( T1> ) T2> ¶ T3>
返回一个新的可选项,并将'use_labels'标志设置为True。
这将导致使用标签对其表名称生成列表达式,例如“SELECT somecolumn AS tablename_somecolumn”。这允许包含多个FROM子句的可选项生成一组唯一的列名称,而不考虑各个FROM子句之间的名称冲突。
as_scalar T0> ( T1> ) T2> ¶ T3>
- 继承自
as_scalar()
方法SelectBase
返回这个可选项的'标量'表示,它可以用作列表达式。
通常,在其子列中只有一列的select语句可以用作标量表达式。
返回的对象是
ScalarSelect
的一个实例。
自动提交 T0> ( T1> ) T2> ¶ T3>
- 继承自
autocommit()
方法SelectBase
将'autocommit'标志设置为True返回一个新的可选项。
从版本0.6开始弃用:
autocommit()
已弃用。使用带有'autocommit'标志的Executable.execution_options()
。
结合 T0> ¶ T1>
- 继承自
bind
属性Executable
返回此
Executable
绑定到的Engine
或Connection
;如果没有找到,则返回None。这是遍历,在本地进行检查,然后检查关联对象的“from”子句,直到找到绑定的引擎或连接。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
-
cte
(name=None, recursive=False)¶ 返回一个新的
CTE
或公共表表达式实例。公用表表达式是一种SQL标准,通过使用一个名为“WITH”的子句,SELECT语句可以使用与主语句一起指定的次要语句。有关UNION的特殊语义也可用于允许“递归”查询,其中SELECT语句可以在先前已选择的一组行上进行绘制。
CTE也可以应用于DML构造对某些数据库的UPDATE,INSERT和DELETE,与RETURNING一起作为CTE行的来源以及CTE行的使用者。
SQLAlchemy将
CTE
对象检测为与Alias
对象类似的对象,作为要传递到语句的FROM子句的特殊元素以及顶部的WITH子句的声明。在版本1.1中进行了更改:添加了对CTE,CTE添加到UPDATE / INSERT / DELETE的UPDATE / INSERT / DELETE的支持。
参数: 以下示例包含两篇来自Postgresql的文档,位于http://www.postgresql.org/docs/current/static/queries-with.html以及其他示例。
例1,非递归:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select([ orders.c.region, func.sum(orders.c.amount).label('total_sales') ]).group_by(orders.c.region).cte("regional_sales") top_regions = select([regional_sales.c.region]).\ where( regional_sales.c.total_sales > select([ func.sum(regional_sales.c.total_sales)/10 ]) ).cte("top_regions") statement = select([ orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ]).where(orders.c.region.in_( select([top_regions.c.region]) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
例2,与RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select([ parts.c.sub_part, parts.c.part, parts.c.quantity]).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select([ parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ]). where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select([ included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ]).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
例3,使用带CTE的UPDATE和INSERT的upsert:
orders = table( 'orders', column('region'), column('amount'), column('product'), column('quantity') ) upsert = ( orders.update() .where(orders.c.region == 'Region1') .values(amount=1.0, product='Product1', quantity=1) .returning(*(orders.c._all_columns)).cte('upsert')) insert = orders.insert().from_select( orders.c.keys(), select([ literal('Region1'), literal(1.0), literal('Product1'), literal(1) ).where(exists(upsert.select())) ) connection.execute(insert)
也可以看看
orm.query.Query.cte()
-HasCTE.cte()
的ORM版本。
描述 T0> ¶ T1>
- 从
description
继承FromClause
这个FromClause的简要描述。
主要用于错误消息格式。
执行 tt> ( * multiparams,** params ) T5>
- 继承自
execute()
方法Executable
编译并执行
Executable
。
execution_options T0> ( T1> **千瓦 T2> ) T3> ¶ T4>
- 继承自
execution_options()
方法 tt>Executable
为执行期间生效的语句设置非SQL选项。
执行选项可以在每个语句或每个
Connection
的基础上设置。此外,Engine
和ORMQuery
对象提供对执行选项的访问,而这些执行选项在连接时进行配置。execution_options()
方法是生成的。返回此语句的新实例,其中包含以下选项:statement = select([table.c.x, table.c.y]) statement = statement.execution_options(autocommit=True)
请注意,只有一部分可能的执行选项可以应用于语句 - 这些选项包括“autocommit”和“stream_results”,但不包括“isolation_level”或“compiled_cache”。有关可能的选项的完整列表,请参阅
Connection.execution_options()
。
FOR_UPDATE T0> ¶ T1>
为
for_update
属性提供传统方言支持。
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
get_children T0> ( T1> ** kwargs T2> ) T3> ¶ T4>
- inherited from the
get_children()
method ofClauseElement
返回这个
ClauseElement
的直接子元素。这用于访问遍历。
** kwargs可能包含更改返回的集合的标志,例如为了减少更大的遍历而返回项目的子集,或者从不同的上下文返回子项目(例如模式级集合而不是子句-水平)。
GROUP_BY T0> ( T1> *条款 T2> ) T3> ¶ T4>
返回一个新的可选项,其中应用了GROUP BY标准的给定列表。
该标准将被附加到任何预先存在的GROUP BY标准。
is_derived_from T0> ( T1> fromclause T2> ) T3> ¶ T4>
- 继承自
is_derived_from()
方法FromClause
如果FromClause从给定的FromClause中“派生”,则返回True。
一个例子是从表中派生的表的别名。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
标签 T0> ( T1> 名称 T2> ) T3> ¶ T4>
- 继承自
label()
方法SelectBase
返回这个可选择的“标量”表示,嵌入为带有标签的子查询。
也可以看看
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
限制 T0> ( T1> 限制 T2> ) T3> ¶ T4>
返回一个新的可选择的给定LIMIT标准。
这是一个数值,通常在结果选择中呈现为
LIMIT
表达式。不支持LIMIT
的后端将尝试提供类似的功能。版本1.0.0更改: -
Select.limit()
现在可以接受任意SQL表达式以及整数值。参数: limit¶ – an integer LIMIT parameter, or a SQL expression that provides an integer result.
偏移 T0> ( T1> 偏移 T2> ) T3> ¶ T4>
使用给定的OFFSET标准返回一个新的可选项。
This is a numeric value which usually renders as an
OFFSET
expression in the resulting select. 不支持OFFSET
的后端将尝试提供类似的功能。版本1.0.0更改: -
Select.offset()
现在可以接受任意SQL表达式以及整数值。参数: offset¶ – an integer OFFSET parameter, or a SQL expression that provides an integer result.
ORDER_BY T0> ( T1> *条款 T2> ) T3> ¶ T4>
返回一个新的可选项,其中应用了ORDER BY标准的给定列表。
该标准将被附加到任何预先存在的ORDER BY标准。
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
标量
( * multiparams,** params ) T5>- inherited from the
scalar()
method ofExecutable
编译并执行此
Executable
,返回结果的标量表示。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
self_group T0> ( T1> 针对=无 T2> ) T3> ¶ T4>
- inherited from the
self_group()
method ofClauseElement
对这个
ClauseElement
应用“分组”。子类重写此方法以返回“分组”结构,即括号。In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()
constructs when placed into the FROM clause of anotherselect()
. (请注意,通常应使用Select.alias()
方法创建子查询,因为许多平台需要命名嵌套的SELECT语句)。由于表达式组合在一起,所以
self_group()
的应用程序是自动的 - 最终用户代码不需要直接使用此方法。Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)
- AND takes precedence over OR.ClauseElement
的baseself_group()
方法仅返回self。
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
-
with_for_update
(nowait=False, read=False, of=None, skip_locked=False, key_share=False)¶ 为
GenerativeSelect
指定一个FOR UPDATE
子句。例如。:
stmt = select([table]).with_for_update(nowait=True)
在像Postgresql或Oracle这样的数据库上,上面的代码会显示如下的语句:
SELECT table.a, table.b FROM table FOR UPDATE NOWAIT
在其他后端,
nowait
选项被忽略,而是产生:SELECT table.a, table.b FROM table FOR UPDATE
当不带任何参数调用时,语句将以后缀
FOR UPDATE
进行呈现。然后可以提供其他参数,这些参数允许使用通用数据库特定的变体。参数: - nowait ¶ - boolean;将在Oracle和Postgresql方言中呈现
FOR tt> UPDATE NOWAIT
- 读 ¶ - boolean;将在MySQL上呈现
LOCK IN SHARE 模式
,在Postgresql上共享 共享
。On Postgresql, when combined withnowait
, will renderFOR SHARE NOWAIT
. - of¶ – SQL expression or list of SQL expression elements (typically
Column
objects or a compatible expression) which will render into aFOR UPDATE OF
clause; supported by PostgreSQL and Oracle. 可以根据后端呈现为表格或列。 - skip_locked ¶ -
boolean, will render
FOR UPDATE SKIP LOCKED
on Oracle and Postgresql dialects orFOR SHARE SKIP LOCKED
ifread=True
is also specified.版本1.1.0中的新功能
- key_share ¶ -
boolean, will render
FOR NO KEY UPDATE
, or if combined withread=True
will renderFOR KEY SHARE
, on the Postgresql dialect.版本1.1.0中的新功能
- nowait ¶ - boolean;将在Oracle和Postgresql方言中呈现
-
- class
sqlalchemy.sql.expression。
HasCTE
¶ Mixin声明一个类包含CTE支持。
版本1.1中的新功能
-
cte
(name=None, recursive=False)¶ 返回一个新的
CTE
或公共表表达式实例。公用表表达式是一种SQL标准,通过使用一个名为“WITH”的子句,SELECT语句可以使用与主语句一起指定的次要语句。有关UNION的特殊语义也可用于允许“递归”查询,其中SELECT语句可以在先前已选择的一组行上进行绘制。
CTE也可以应用于DML构造对某些数据库的UPDATE,INSERT和DELETE,与RETURNING一起作为CTE行的来源以及CTE行的使用者。
SQLAlchemy将
CTE
对象检测为与Alias
对象类似的对象,作为要传递到语句的FROM子句的特殊元素以及顶部的WITH子句的声明。在版本1.1中进行了更改:添加了对CTE,CTE添加到UPDATE / INSERT / DELETE的UPDATE / INSERT / DELETE的支持。
参数: 以下示例包含两篇来自Postgresql的文档,位于http://www.postgresql.org/docs/current/static/queries-with.html以及其他示例。
例1,非递归:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select([ orders.c.region, func.sum(orders.c.amount).label('total_sales') ]).group_by(orders.c.region).cte("regional_sales") top_regions = select([regional_sales.c.region]).\ where( regional_sales.c.total_sales > select([ func.sum(regional_sales.c.total_sales)/10 ]) ).cte("top_regions") statement = select([ orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ]).where(orders.c.region.in_( select([top_regions.c.region]) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
例2,与RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select([ parts.c.sub_part, parts.c.part, parts.c.quantity]).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select([ parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ]). where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select([ included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ]).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
例3,使用带CTE的UPDATE和INSERT的upsert:
orders = table( 'orders', column('region'), column('amount'), column('product'), column('quantity') ) upsert = ( orders.update() .where(orders.c.region == 'Region1') .values(amount=1.0, product='Product1', quantity=1) .returning(*(orders.c._all_columns)).cte('upsert')) insert = orders.insert().from_select( orders.c.keys(), select([ literal('Region1'), literal(1.0), literal('Product1'), literal(1) ).where(exists(upsert.select())) ) connection.execute(insert)
也可以看看
orm.query.Query.cte()
-HasCTE.cte()
的ORM版本。
-
- class
sqlalchemy.sql.expression。
HasPrefixes
¶ prefix_with
( * expr,** kw ) T5>在语句关键字后添加一个或多个表达式,即SELECT,INSERT,UPDATE或DELETE。生成。
这用于支持后端特定的前缀关键字,例如由MySQL提供的前缀关键字。
例如。:
stmt = table.insert().prefix_with("LOW_PRIORITY", dialect="mysql")
可以通过多次调用
prefix_with()
来指定多个前缀。参数: - *expr¶ – textual or
ClauseElement
construct which will be rendered following the INSERT, UPDATE, or DELETE keyword. - ** kw ¶ - 接受单个关键字'dialect'。这是一个可选的字符串方言名称,它将限制将该前缀的呈现仅限于该方言。
- *expr¶ – textual or
- class
sqlalchemy.sql.expression。
HasSuffixes
¶ 后缀 tt> ( * expr,** kw ) T5>
在整个语句后添加一个或多个表达式。
这用于在特定结构上支持后端特定的后缀关键字。
例如。:
stmt = select([col1, col2]).cte().suffix_with( "cycle empno set y_cycle to 1 default 0", dialect="oracle")
多个后缀可以通过多次调用
suffix_with()
来指定。参数: - *expr¶ – textual or
ClauseElement
construct which will be rendered following the target clause. - ** kw ¶ - 接受单个关键字'dialect'。这是一个可选的字符串方言名称,它将限制仅将该后缀渲染为该方言。
- *expr¶ – textual or
- class
sqlalchemy.sql.expression.
Join
(left, right, onclause=None, isouter=False, full=False)¶ 基础:
sqlalchemy.sql.expression.FromClause
represent a
JOIN
construct between twoFromClause
elements.The public constructor function for
Join
is the module-leveljoin()
function, as well as theFromClause.join()
method of anyFromClause
(e.g. such asTable
).-
__init__
(left, right, onclause=None, isouter=False, full=False)¶ 构建一个新的
Join
。这里通常的入口点是任何
FromClause
对象的join()
函数或FromClause.join()
方法。
-
alias
(name=None, flat=False)¶ 返回此
Join
的别名。这里的默认行为是首先从
Join
生成一个SELECT构造,然后从中产生一个Alias
。所以给了一个形式的加入:j = table_a.join(table_b, table_a.c.id == table_b.c.a_id)
本身的JOIN看起来像:
table_a JOIN table_b ON table_a.id = table_b.a_id
Whereas the alias of the above,
j.alias()
, would in a SELECT context look like:(SELECT table_a.id AS table_a_id, table_b.id AS table_b_id, table_b.a_id AS table_b_a_id FROM table_a JOIN table_b ON table_a.id = table_b.a_id) AS anon_1
给定一个
Join
对象j
,等价的长手形式是:from sqlalchemy import select, alias j = alias( select([j.left, j.right]).\ select_from(j).\ with_labels(True).\ correlate(False), name=name )
由
Join.alias()
生成的可选择列与以单个名称显示的两个单独可选列的列相同 - 各列为“自动标记”,即.c.
所得到的Alias
的集合使用<tablename>_<columname>
方案表示各个列的名称:j.c.table_a_id j.c.table_b_a_id
Join.alias()
还具有替代选项,用于别名联接,不会产生封闭的SELECT,并且通常不会将标签应用于列名称。flat=True
选项将分别针对左侧和右侧调用FromClause.alias()
。使用这个选项,不会产生新的SELECT
;我们相反,从一个构造如下:j = table_a.join(table_b, table_a.c.id == table_b.c.a_id) j = j.alias(flat=True)
我们得到如下结果:
table_a AS table_a_1 JOIN table_b AS table_b_1 ON table_a_1.id = table_b_1.a_id
flat=True
参数也会传播到包含的selectables,以便组合连接如:j = table_a.join( table_b.join(table_c, table_b.c.id == table_c.c.b_id), table_b.c.a_id == table_a.c.id ).alias(flat=True)
会产生如下表达式:
table_a AS table_a_1 JOIN ( table_b AS table_b_1 JOIN table_c AS table_c_1 ON table_b_1.id = table_c_1.b_id ) ON table_a_1.id = table_b_1.a_id
独立的
alias()
函数以及基础的FromClause.alias()
方法也支持flat=True
参数作为无操作,以便参数可以传递给任何可选择的alias()
方法。版本0.9.0新增:增加了
flat=True
选项来创建连接的“别名”,而不用在SELECT子查询内部进行封闭。参数: 也可以看看
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
选择
( whereclause = None,** kwargs ) / T5>给定一个
Join
对象j
,等价的长手形式是:from sqlalchemy import select j = select([j.left, j.right], **kw).\ where(whereclause).\ select_from(j)
参数:
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
-
- class
sqlalchemy.sql.expression.
Lateral
(selectable, name=None)¶ 基础:
sqlalchemy.sql.expression.Alias
表示一个LATERAL子查询。
This object is constructed from the
lateral()
module level function as well as theFromClause.lateral()
method available on allFromClause
subclasses.尽管LATERAL是SQL标准的一部分,但只有更新的Postgresql版本才支持此关键字。
版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
-
- class
sqlalchemy.sql.expression.
ScalarSelect
(element)¶ 基础:
sqlalchemy.sql.expression.Generative
,sqlalchemy.sql.expression.Grouping
其中 T0> ( T1> 暴 T2> ) T3> ¶ T4>
对此
ScalarSelect
引用的SELECT语句应用WHERE子句。
- class
sqlalchemy.sql.expression。
选择
( columns =无, whereclause = None,from_obj = None,distinct = False,具有=无,correlate = t9>,前缀=无,后缀=无,** kwargs ) > 基础:
sqlalchemy.sql.expression.HasPrefixes
,sqlalchemy.sql.expression.HasSuffixes
,sqlalchemy.sql.expression.GenerativeSelect
代表一个
SELECT
语句。-
__init__
(columns=None, whereclause=None, from_obj=None, distinct=False, having=None, correlate=True, prefixes=None, suffixes=None, **kwargs)¶ 构建一个新的
Select
对象。这个构造函数被镜像为公共API函数;有关完整的用法和参数说明,请参阅
select()
。
-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
append_column T0> ( T1> 列 T2> ) T3> ¶ T4>
将给定的列表达式追加到此select()构造的columns子句中。
这是一种in-place突变方法;
column()
方法是首选,因为它提供了标准的method chaining。
append_correlation T0> ( T1> fromclause T2> ) T3> ¶ T4>
将给定的相关表达式附加到这个select()结构中。
这是一种in-place突变方法;
correlate()
方法是首选,因为它提供了标准的method chaining。
append_from T0> ( T1> fromclause T2> ) T3> ¶ T4>
将给定的FromClause表达式附加到这个select()构造的FROM子句中。
这是一种in-place突变方法;
select_from()
方法是首选,因为它提供了标准的method chaining。
append_group_by T0> ( T1> *条款 T2> ) T3> ¶ T4>
追加应用于此可选项的给定GROUP BY标准。
该标准将被附加到任何预先存在的GROUP BY标准。
这是一种in-place突变方法;
group_by()
方法是首选,因为它提供了标准的method chaining。
append_having T0> ( T1> 具有 T2> ) T3> ¶ T4>
将给定的表达式追加到这个select()构造的HAVING标准中。
该表达式将通过AND连接到现有的HAVING标准。
This is an in-place mutation method; the
having()
method is preferred, as it provides standard method chaining.
append_order_by T0> ( T1> *条款 T2> ) T3> ¶ T4>
附加给定的ORDER BY标准应用于此可选项。
该标准将被附加到任何预先存在的ORDER BY标准。
这是一种in-place突变方法;
order_by()
方法是首选,因为它提供了标准的method chaining。
append_prefix T0> ( T1> 子句 T2> ) T3> ¶ T4>
将给定的列子句前缀表达式附加到此select()构造。
这是一种in-place突变方法;
prefix_with()
方法是首选,因为它提供了标准的method chaining。
append_whereclause T0> ( T1> whereclause T2> ) T3> ¶ T4>
将给定的表达式追加到这个select()构造的WHERE标准中。
该表达式将通过AND连接到现有的WHERE标准。
这是一种in-place突变方法;
where()
方法是首选,因为它提供标准的method chaining。
apply_labels T0> ( T1> ) T2> ¶ T3>
- 继承自
apply_labels()
方法GenerativeSelect
返回一个新的可选项,并将'use_labels'标志设置为True。
这将导致使用标签对其表名称生成列表达式,例如“SELECT somecolumn AS tablename_somecolumn”。这允许包含多个FROM子句的可选项生成一组唯一的列名称,而不考虑各个FROM子句之间的名称冲突。
as_scalar T0> ( T1> ) T2> ¶ T3>
- 继承自
as_scalar()
方法SelectBase
返回这个可选项的'标量'表示,它可以用作列表达式。
通常,在其子列中只有一列的select语句可以用作标量表达式。
返回的对象是
ScalarSelect
的一个实例。
自动提交 T0> ( T1> ) T2> ¶ T3>
- 继承自
autocommit()
方法SelectBase
将'autocommit'标志设置为True返回一个新的可选项。
从版本0.6开始弃用:
autocommit()
已弃用。使用带有'autocommit'标志的Executable.execution_options()
。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ( T1> 列 T2> ) T3> ¶ T4>
返回一个新的select()构造,并将给定的列表达式添加到它的columns子句中。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
归属关系 T0> ( T1> * fromclauses T2> ) T3> ¶ T4>
返回一个新的
Select
,它将给定的FROM子句与一个包含Select
的FROM子句关联起来。调用此方法会关闭
Select
对象的“自动关联”的默认行为。通常,出现在Select
中的FROM元素将通过它的WHERE clause,ORDER BY,HAVING或columns clause这个Select
对象的FROM clause。使用Select.correlate()
方法设置显式相关集合,可以提供一个固定的FROM对象列表,这些对象可能会在此过程中发生。当使用
Select.correlate()
应用特定的FROM子句进行关联时,无论此Select
对象相对于将Select
。这与“自动关联”的行为形成鲜明对比,该行为仅与立即包含Select
相关。多级关联确保封闭和封闭Select
之间的链接始终通过至少一个WHERE / ORDER BY / HAVING / columns子句进行关联。如果传递
None
,则Select
对象不会关联任何FROM条目,并且所有对象都将无条件地呈现在本地FROM子句中。参数: * fromclauses ¶ - a list of one or more
FromClause
constructs, or other compatible constructs (i.e. ORM-mapped classes) to become part of the correlate collection.在版本0.8.0中更改: ORM映射类被
Select.correlate()
接受。版本0.8.0中已更改:
Select.correlate()
方法不再无条件地从FROM子句中删除条目;相反,候选FROM条目还必须与位于封闭Select
中的FROM条目匹配,该条目最终包含在WHERE子句,ORDER BY子句,HAVING子句或columns子句中一个封闭的Select()
。
correlate_except T0> ( T1> * fromclauses T2> ) T3> ¶ T4>
返回一个新的
Select
,它将从自动关联过程中省略给定的FROM子句。调用
Select.correlate_except()
会关闭给定FROM元素的Select
对象的“自动关联”默认行为。此处指定的元素将无条件出现在FROM列表中,而所有其他FROM元素仍保持正常的自动关联行为。在0.8.2版本中进行了更改:对
Select.correlate_except()
方法进行了改进,以完全防止此处指定的FROM子句从此Select
如果传递
None
,则Select
对象将关联其所有FROM条目。版本0.8.2更改:调用
correlate_except(None)
将正确地自动关联所有FROM子句。参数: *fromclauses¶ – a list of one or more FromClause
constructs, or other compatible constructs (i.e. ORM-mapped classes) to become part of the correlate-exception collection.
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
-
cte
(name=None, recursive=False)¶ 返回一个新的
CTE
或公共表表达式实例。公用表表达式是一种SQL标准,通过使用一个名为“WITH”的子句,SELECT语句可以使用与主语句一起指定的次要语句。有关UNION的特殊语义也可用于允许“递归”查询,其中SELECT语句可以在先前已选择的一组行上进行绘制。
CTE也可以应用于DML构造对某些数据库的UPDATE,INSERT和DELETE,与RETURNING一起作为CTE行的来源以及CTE行的使用者。
SQLAlchemy将
CTE
对象检测为与Alias
对象类似的对象,作为要传递到语句的FROM子句的特殊元素以及顶部的WITH子句的声明。在版本1.1中进行了更改:添加了对CTE,CTE添加到UPDATE / INSERT / DELETE的UPDATE / INSERT / DELETE的支持。
参数: 以下示例包含两篇来自Postgresql的文档,位于http://www.postgresql.org/docs/current/static/queries-with.html以及其他示例。
例1,非递归:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select([ orders.c.region, func.sum(orders.c.amount).label('total_sales') ]).group_by(orders.c.region).cte("regional_sales") top_regions = select([regional_sales.c.region]).\ where( regional_sales.c.total_sales > select([ func.sum(regional_sales.c.total_sales)/10 ]) ).cte("top_regions") statement = select([ orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ]).where(orders.c.region.in_( select([top_regions.c.region]) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
例2,与RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select([ parts.c.sub_part, parts.c.part, parts.c.quantity]).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select([ parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ]). where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select([ included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ]).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
例3,使用带CTE的UPDATE和INSERT的upsert:
orders = table( 'orders', column('region'), column('amount'), column('product'), column('quantity') ) upsert = ( orders.update() .where(orders.c.region == 'Region1') .values(amount=1.0, product='Product1', quantity=1) .returning(*(orders.c._all_columns)).cte('upsert')) insert = orders.insert().from_select( orders.c.keys(), select([ literal('Region1'), literal(1.0), literal('Product1'), literal(1) ).where(exists(upsert.select())) ) connection.execute(insert)
也可以看看
orm.query.Query.cte()
-HasCTE.cte()
的ORM版本。
描述 T0> ¶ T1>
- 从
description
继承FromClause
这个FromClause的简要描述。
主要用于错误消息格式。
不同 T0> ( T1> * EXPR T2> ) T3> ¶ T4>
返回一个新的select()构造,它将DISTINCT应用到它的columns子句。
参数: * expr ¶ - 可选的列表达式。当存在时,Postgresql方言将呈现 DISTINCT ON (&lt;表达式&gt;)
结构。
-
except_
(other, **kwargs)¶ 针对给定的selectable返回此select()构造的SQL EXCEPT。
except_all
( 其他,** kwargs ) t5 >根据给定的selectable返回SQL EXCEPT ALL的所有select()构造。
执行 tt> ( * multiparams,** params ) T5>
- 继承自
execute()
方法Executable
编译并执行
Executable
。
execution_options T0> ( T1> **千瓦 T2> ) T3> ¶ T4>
- 继承自
execution_options()
方法 tt>Executable
为执行期间生效的语句设置非SQL选项。
执行选项可以在每个语句或每个
Connection
的基础上设置。此外,Engine
和ORMQuery
对象提供对执行选项的访问,而这些执行选项在连接时进行配置。execution_options()
方法是生成的。返回此语句的新实例,其中包含以下选项:statement = select([table.c.x, table.c.y]) statement = statement.execution_options(autocommit=True)
请注意,只有一部分可能的执行选项可以应用于语句 - 这些选项包括“autocommit”和“stream_results”,但不包括“isolation_level”或“compiled_cache”。有关可能的选项的完整列表,请参阅
Connection.execution_options()
。
FOR_UPDATE T0> ¶ T1>
- 继承自
for_update
属性GenerativeSelect
为
for_update
属性提供传统方言支持。
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
320交织 T0> ¶ T1>
返回FromClause元素的显示列表。
get_children
( column_collections = True,** kwargs ) / T5>按照ClauseElement规范返回子元素。
GROUP_BY T0> ( T1> *条款 T2> ) T3> ¶ T4>
- 从
group_by()
方法继承的GenerativeSelect
返回一个新的可选项,其中应用了GROUP BY标准的给定列表。
该标准将被附加到任何预先存在的GROUP BY标准。
具有 T0> ( T1> 具有 T2> ) T3> ¶ T4>
返回一个新的select()构造,将给定的表达式添加到它的HAVING子句中,通过AND连接到现有子句(如果有的话)。
inner_columns T0> ¶ T1>
所有ColumnElement表达式的迭代器,这些表达式将被渲染到结果SELECT语句的columns子句中。
相交 T0> ( T1> 其他 T2>, ** kwargs T3> ) T4> ¶ T5 >
根据给定的selectable返回此select()构造的SQL INTERSECT。
intersect_all
( 其他,** kwargs ) t5 >根据给定的selectable返回一个SQL INTERSECT ALL这个select()构造。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
标签 T0> ( T1> 名称 T2> ) T3> ¶ T4>
- 继承自
label()
方法SelectBase
返回这个可选择的“标量”表示,嵌入为带有标签的子查询。
也可以看看
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
限制 T0> ( T1> 限制 T2> ) T3> ¶ T4>
- inherited from the
limit()
method ofGenerativeSelect
返回一个新的可选择的给定LIMIT标准。
这是一个数值,通常在结果选择中呈现为
LIMIT
表达式。不支持LIMIT
的后端将尝试提供类似的功能。版本1.0.0更改: -
Select.limit()
现在可以接受任意SQL表达式以及整数值。参数: limit¶ – an integer LIMIT parameter, or a SQL expression that provides an integer result.
-
locate_all_froms
(*args, **kw)¶ 返回此Select所引用的所有FromClause元素的集合。
这个集合是由
froms
属性返回的超集,这个属性专门用于那些实际将被渲染的FromClause元素。
偏移 T0> ( T1> 偏移 T2> ) T3> ¶ T4>
- inherited from the
offset()
method ofGenerativeSelect
使用给定的OFFSET标准返回一个新的可选项。
This is a numeric value which usually renders as an
OFFSET
expression in the resulting select. 不支持OFFSET
的后端将尝试提供类似的功能。版本1.0.0更改: -
Select.offset()
现在可以接受任意SQL表达式以及整数值。参数: offset¶ – an integer OFFSET parameter, or a SQL expression that provides an integer result.
ORDER_BY T0> ( T1> *条款 T2> ) T3> ¶ T4>
- inherited from the
order_by()
method ofGenerativeSelect
返回一个新的可选项,其中应用了ORDER BY标准的给定列表。
该标准将被附加到任何预先存在的ORDER BY标准。
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
prefix_with
( * expr,** kw ) T5>- 继承自
prefix_with()
方法HasPrefixes
在语句关键字后添加一个或多个表达式,即SELECT,INSERT,UPDATE或DELETE。生成。
这用于支持后端特定的前缀关键字,例如由MySQL提供的前缀关键字。
例如。:
stmt = table.insert().prefix_with("LOW_PRIORITY", dialect="mysql")
可以通过多次调用
prefix_with()
来指定多个前缀。参数: - *expr¶ – textual or
ClauseElement
construct which will be rendered following the INSERT, UPDATE, or DELETE keyword. - ** kw ¶ - 接受单个关键字'dialect'。这是一个可选的字符串方言名称,它将限制将该前缀的呈现仅限于该方言。
- *expr¶ – textual or
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
reduce_columns T0> ( T1> only_synonyms =真 T2> ) T3> ¶ T4>
返回一个新的:func`.select`构造,其中包含从列子句中删除的冗余命名的等价值列。
这里的“冗余”表示两列中的一列是基于外键引用另一列,或者通过语句的WHERE子句中的简单相等比较来引用。The primary purpose of this method is to automatically construct a select statement with all uniquely-named columns, without the need to use table-qualified labels as
apply_labels()
does.当根据外键省略列时,被引用的列是保留的列。当基于WHERE eqivalence省略列时,columns子句中的第一列是保留的列。
参数: only_synonyms¶ – when True, limit the removal of columns to those which have the same name as the equivalent. 否则,将删除所有与另一个等效的列。 0.8版本中的新功能
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
标量
( * multiparams,** params ) T5>- inherited from the
scalar()
method ofExecutable
编译并执行此
Executable
,返回结果的标量表示。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
select_from T0> ( T1> fromclause T2> ) T3> ¶ T4>
返回一个新的
select()
结构,并将给定的FROM表达式合并到它的FROM对象列表中。例如。:
table1 = table('t1', column('a')) table2 = table('t2', column('b')) s = select([table1.c.a]).\ select_from( table1.join(table2, table1.c.a==table2.c.b) )
“from”列表是每个元素身份的唯一集合,因此添加一个已经存在的
Table
或其他可选项将不起作用。传递引用已存在的Table
或其他可选项的Join
将会在呈现的FROM列表中隐藏作为单个元素可选择的存在,而不是呈现它变成一个JOIN子句。尽管
Select.select_from()
的典型用途是用连接替换默认的派生FROM子句,但如果需要,也可以使用单个表元素多次调用它,如果需要的话FROM子句不能从columns子句中完全派生:select([func.count('*')]).select_from(table1)
self_group T0> ( T1> 针对=无 T2> ) T3> ¶ T4>
按照ClauseElement规范返回“分组”结构。
这产生了一个可以嵌入到表达式中的元素。请注意,在构建表达式时,会根据需要自动调用此方法,并且不需要明确使用。
后缀 tt> ( * expr,** kw ) T5>
- inherited from the
suffix_with()
method ofHasSuffixes
在整个语句后添加一个或多个表达式。
这用于在特定结构上支持后端特定的后缀关键字。
例如。:
stmt = select([col1, col2]).cte().suffix_with( "cycle empno set y_cycle to 1 default 0", dialect="oracle")
多个后缀可以通过多次调用
suffix_with()
来指定。参数: - *expr¶ – textual or
ClauseElement
construct which will be rendered following the target clause. - ** kw ¶ - 接受单个关键字'dialect'。这是一个可选的字符串方言名称,它将限制仅将该后缀渲染为该方言。
- *expr¶ – textual or
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
union
( 其他,** kwargs ) t5 >根据给定的selectable返回此select()构造的SQL UNION。
union_all
( 其他,** kwargs ) t5 >针对给定的selectable返回此select()构造的SQL UNION ALL。
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
其中 T0> ( T1> whereclause T2> ) T3> ¶ T4>
返回一个新的select()构造,将给定的表达式添加到其WHERE子句中,通过AND连接到现有子句(如果有的话)。
-
with_for_update
(nowait=False, read=False, of=None, skip_locked=False, key_share=False)¶ - inherited from the
with_for_update()
method ofGenerativeSelect
为
GenerativeSelect
指定一个FOR UPDATE
子句。例如。:
stmt = select([table]).with_for_update(nowait=True)
在像Postgresql或Oracle这样的数据库上,上面的代码会显示如下的语句:
SELECT table.a, table.b FROM table FOR UPDATE NOWAIT
在其他后端,
nowait
选项被忽略,而是产生:SELECT table.a, table.b FROM table FOR UPDATE
当不带任何参数调用时,语句将以后缀
FOR UPDATE
进行呈现。然后可以提供其他参数,这些参数允许使用通用数据库特定的变体。参数: - nowait ¶ - boolean;将在Oracle和Postgresql方言中呈现
FOR tt> UPDATE NOWAIT
- 读 ¶ - boolean;将在MySQL上呈现
LOCK IN SHARE 模式
,在Postgresql上共享 共享
。On Postgresql, when combined withnowait
, will renderFOR SHARE NOWAIT
. - of¶ – SQL expression or list of SQL expression elements (typically
Column
objects or a compatible expression) which will render into aFOR UPDATE OF
clause; supported by PostgreSQL and Oracle. 可以根据后端呈现为表格或列。 - skip_locked ¶ -
boolean, will render
FOR UPDATE SKIP LOCKED
on Oracle and Postgresql dialects orFOR SHARE SKIP LOCKED
ifread=True
is also specified.版本1.1.0中的新功能
- key_share ¶ -
boolean, will render
FOR NO KEY UPDATE
, or if combined withread=True
will renderFOR KEY SHARE
, on the Postgresql dialect.版本1.1.0中的新功能
- nowait ¶ - boolean;将在Oracle和Postgresql方言中呈现
-
with_hint
(selectable, text, dialect_name='*')¶ 给这个
Select
添加一个索引或其他执行上下文提示。相对于给定的
Table
或Alias
作为selectable
传递,提示文本呈现在正在使用的数据库后端的适当位置。论据。方言实现通常使用Python字符串替换语法和令牌%(name)s
来呈现表或别名的名称。例如。在使用Oracle时,需要:select([mytable]).\ with_hint(mytable, "index(%(name)s ix_mytable)")
将SQL呈现为:
select /*+ index(mytable ix_mytable) */ ... from mytable
The
dialect_name
option will limit the rendering of a particular hint to a particular backend. 例如,同时为Oracle和Sybase添加提示:select([mytable]).\ with_hint(mytable, "index(%(name)s ix_mytable)", 'oracle').\ with_hint(mytable, "WITH INDEX ix_mytable", 'sybase')
with_only_columns T0> ( T1> 列 T2> ) T3> ¶ T4>
返回一个新的
select()
结构,其中的columns子句替换为给定的列。版本0.7.3更改:由于错误修复,此方法在版本0.7.3中有轻微的行为改变。在版本0.7.3之前,已经预先计算了
select()
的FROM子句,并添加了新列;在0.7.3及更高版本中,它是在编译时计算的,解决了将列与父表后期绑定的问题。这改变了Select.with_only_columns()
的行为,因为删除了不再在新列表中表示的FROM子句,但是这种行为更一致,因为FROM子句始终从当前列子句派生。这种方法的最初意图是允许修剪现有列列表的列数比原来存在的列数少;在完全不同的列表中替换列表的用例在0.7.3发布之前一直没有预料到;下面的使用指南说明了这应该如何完成。这个方法与使用给定的columns子句调用原始的
select()
完全等价。即一份声明:s = select([table1.c.a, table1.c.b]) s = s.with_only_columns([table1.c.b])
应该完全等同于:
s = select([table1.c.b])
这意味着只有从列列表派生的FROM子句将被丢弃,如果新列列表不再包含该FROM:
>>> table1 = table('t1', column('a'), column('b')) >>> table2 = table('t2', column('a'), column('b')) >>> s1 = select([table1.c.a, table2.c.b]) >>> print s1 SELECT t1.a, t2.b FROM t1, t2 >>> s2 = s1.with_only_columns([table2.c.b]) >>> print s2 SELECT t2.b FROM t1
在构造中维护特定FROM子句的首选方法,假设它不会在其他地方表示(即不在WHERE子句中等)是使用
Select.select_from()
来设置它:>>> s1 = select([table1.c.a, table2.c.b]).\ ... select_from(table1.join(table2, ... table1.c.a==table2.c.a)) >>> s2 = s1.with_only_columns([table2.c.b]) >>> print s2 SELECT t2.b FROM t1 JOIN t2 ON t1.a=t2.a
还应注意使用传递给
Select.with_only_columns()
的正确的一组列对象。由于该方法基本上等效于首先用给定列调用select()
构造,传递给Select.with_only_columns()
的列通常应该是一个子集传递给select()
构造的那些,而不是那些可从select()
的.c
集合中获得的构造。那是:s = select([table1.c.a, table1.c.b]).select_from(table1) s = s.with_only_columns([table1.c.b])
和不是:
# usually incorrect s = s.with_only_columns([s.c.b])
后者将生成SQL:
SELECT b FROM (SELECT t1.a AS a, t1.b AS b FROM t1), t1
由于
select()
构造基本上被要求从table1
以及其自身中选择。
-
with_statement_hint
(text, dialect_name='*')¶ 向此
Select
添加语句提示。该方法类似于
Select.with_hint()
,不同之处在于它不需要单独的表格,而是作为整体应用于该语句。此处的提示仅针对后端数据库,并可能包含隔离级别,文件指令,提取指令等指令。
版本1.0.0中的新功能
也可以看看
-
- class
sqlalchemy.sql.expression。
可选
¶ 基础:
sqlalchemy.sql.expression.ClauseElement
将课程标记为可选
- class
sqlalchemy.sql.expression。
SelectBase
¶ 基础:
sqlalchemy.sql.expression.HasCTE
,sqlalchemy.sql.expression.Executable
,sqlalchemy.sql.expression.FromClause
SELECT语句的基类。
这包括
Select
,CompoundSelect
和TextAsFrom
。as_scalar T0> ( T1> ) T2> ¶ T3>
返回这个可选项的'标量'表示,它可以用作列表达式。
通常,在其子列中只有一列的select语句可以用作标量表达式。
返回的对象是
ScalarSelect
的一个实例。
自动提交 T0> ( T1> ) T2> ¶ T3>
将'autocommit'标志设置为True返回一个新的可选项。
从版本0.6开始弃用:
autocommit()
已弃用。使用带有'autocommit'标志的Executable.execution_options()
。
标签 T0> ( T1> 名称 T2> ) T3> ¶ T4>
返回这个可选择的“标量”表示,嵌入为带有标签的子查询。
也可以看看
- class
sqlalchemy.sql.expression。
TableClause
( name,*列 T5> ) T6> ¶ T7> 基础:
sqlalchemy.sql.expression.Immutable
,sqlalchemy.sql.expression.FromClause
代表最小的“表”结构。
这是一个轻量级的表格对象,它只有一个名称和一个列集合,通常由
expression.column()
函数生成:from sqlalchemy import table, column user = table("user", column("id"), column("name"), column("description"), )
TableClause
结构可作为更常用的Table
对象的基础,提供常见的FromClause
服务集合,包括.c.
收集和声明生成方法。It does not provide all the additional schema-level services of
Table
, including constraints, references to other tables, or support forMetaData
-level services. 当一个更完全的Table
不在手边时,它可以作为一个临时构造用于生成快速SQL语句。__ init __
( name,*列 )构建一个新的
TableClause
对象。这个构造函数被镜像为公共API函数;有关完整的用法和参数说明,请参阅
table()
。
-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
删除
( whereclause = None,** kwargs ) / T5>根据这个
TableClause
生成一个delete()
结构。例如。:
table.delete().where(table.c.id==7)
有关参数和使用信息,请参阅
delete()
。
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
implicit_returning
= False ¶TableClause
doesn’t support having a primary key or column -level defaults, so implicit returning doesn’t apply.
insert
( values = None,inline = False,** kwargs ) T5> ¶ T6>针对这个
TableClause
生成一个insert()
结构。例如。:
table.insert().values(name='foo')
有关参数和使用信息,请参见
insert()
。
is_derived_from T0> ( T1> fromclause T2> ) T3> ¶ T4>
- 继承自
is_derived_from()
方法FromClause
如果FromClause从给定的FromClause中“派生”,则返回True。
一个例子是从表中派生的表的别名。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
self_group T0> ( T1> 针对=无 T2> ) T3> ¶ T4>
- inherited from the
self_group()
method ofClauseElement
对这个
ClauseElement
应用“分组”。子类重写此方法以返回“分组”结构,即括号。In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()
constructs when placed into the FROM clause of anotherselect()
. (请注意,通常应使用Select.alias()
方法创建子查询,因为许多平台需要命名嵌套的SELECT语句)。由于表达式组合在一起,所以
self_group()
的应用程序是自动的 - 最终用户代码不需要直接使用此方法。Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)
- AND takes precedence over OR.ClauseElement
的baseself_group()
方法仅返回self。
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
-
update
(whereclause=None, values=None, inline=False, **kwargs)¶ 根据这个
TableClause
生成一个update()
结构。例如。:
table.update().where(table.c.id==7).values(name='foo')
有关参数和使用信息,请参阅
update()
。
- class
sqlalchemy.sql.expression.
TableSample
(selectable, sampling, name=None, seed=None)¶ 基础:
sqlalchemy.sql.expression.Alias
表示一个TABLESAMPLE子句。
该对象由所有
FromClause
子类上的tablesample()
模块级函数以及FromClause.tablesample()
方法构造而成。版本1.1中的新功能
也可以看看
-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
-
- class
sqlalchemy.sql.expression。
TextAsFrom
( text,columns ,positions = False ) ¶ 基础:
sqlalchemy.sql.expression.SelectBase
在
SelectBase
界面中包装TextClause
结构。This allows the
TextClause
object to gain a.c
collection and other FROM-like capabilities such asFromClause.alias()
,SelectBase.cte()
, etc.TextAsFrom
结构是通过TextClause.columns()
方法生成的 - 请参阅该方法以获取详细信息。版本0.9.0中的新功能
-
alias
(name=None, flat=False)¶ - 从
alias()
方法继承FromClause
返回
FromClause
的别名。这是调用的简写:
from sqlalchemy import alias a = alias(self, name=name)
有关详细信息,请参阅
alias()
。
as_scalar T0> ( T1> ) T2> ¶ T3>
- 继承自
as_scalar()
方法SelectBase
返回这个可选项的'标量'表示,它可以用作列表达式。
通常,在其子列中只有一列的select语句可以用作标量表达式。
返回的对象是
ScalarSelect
的一个实例。
自动提交 T0> ( T1> ) T2> ¶ T3>
- 继承自
autocommit()
方法SelectBase
将'autocommit'标志设置为True返回一个新的可选项。
从版本0.6开始弃用:
autocommit()
已弃用。使用带有'autocommit'标志的Executable.execution_options()
。
结合 T0> ¶ T1>
- 继承自
bind
属性Executable
返回此
Executable
绑定到的Engine
或Connection
;如果没有找到,则返回None。这是遍历,在本地进行检查,然后检查关联对象的“from”子句,直到找到绑定的引擎或连接。
C T0> ¶ T1>
- inherited from the
c
attribute ofFromClause
columns
属性的别名。
列 T0> ¶ T1>
- inherited from the
columns
attribute ofFromClause
由
FromClause
维护的ColumnElement
对象的基于命名的集合。columns
或c
集合是使用表绑定或其他可选绑定列构建SQL表达式的入口:select([mytable]).where(mytable.c.somecolumn == 5)
比较
( 其他,** kw ) t5 >- inherited from the
compare()
method ofClauseElement
将此ClauseElement与给定的ClauseElement进行比较。
子类应该覆盖默认行为,这是一种直接的身份比较。
** kw是由subclass compare()方法消耗的参数,可用于修改比较条件。(请参阅
ColumnElement
)
编译
( bind = None,dialect = None,** kw ) T5> ¶ T6>- inherited from the
compile()
method ofClauseElement
编译这个SQL表达式。
返回值是一个
Compiled
对象。对返回值调用str()
或unicode()
将产生结果的字符串表示形式。Compiled
对象还可以使用params
访问器返回一个绑定参数名称和值的字典。参数: - bind¶ – An
Engine
orConnection
from which aCompiled
will be acquired. 这个参数优先于这个ClauseElement
的绑定引擎,如果有的话。 - column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. 如果
None
,则呈现目标表格对象中的所有列。 - dialect¶ – A
Dialect
instance from which aCompiled
will be acquired. 该参数优先于bind参数以及ClauseElement
的绑定引擎(如果有的话)。 - inline¶ – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. 这通常是指序列执行,但也可能指与主键Column关联的任何服务器端默认生成函数。
- compile_kwargs ¶ -
在所有“访问”方法中将传递给编译器的附加参数的可选字典。例如,这允许将自定义标志传递给自定义编译构造。它也用于传递
literal_binds
标志的情况:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select([t]).where(t.c.x == 5) print s.compile(compile_kwargs={"literal_binds": True})
版本0.9.0中的新功能
- bind¶ – An
对等元等于
( 列,等值 )- inherited from the
correspond_on_equivalents()
method ofFromClause
返回给定列的相应列,或者如果None搜索给定字典中的匹配项。
对应列
( 列,require_embedded = False ) t5 >- 继承自
corresponding_column()
方法FromClause
给定一个
ColumnElement
,从这个Selectable
对象的原始Column
通过共同的祖先返回导出的ColumnElement
柱。参数: - column¶ – the target
ColumnElement
to be matched - require_embedded¶ – only return corresponding columns for the given
ColumnElement
, if the givenColumnElement
is actually present within a sub-element of thisFromClause
. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisFromClause
.
- column¶ – the target
count
( whereclause = None,** params ) / T5>- 继承自
count()
方法FromClause
返回一个根据
FromClause
生成的SELECT COUNT。从版本1.1开始弃用:
FromClause.count()
已弃用。对行进行计数需要正确的列表达式和联接,DISTINCT等。必须提出,否则结果可能不是预期的结果。请直接使用适当的func.count()
表达式。该函数针对表的主键中的第一列或整个表中的第一列生成COUNT。显式使用
func.count()
应该是首选的:row_count = conn.scalar( select([func.count('*')]).select_from(table) )
也可以看看
-
cte
(name=None, recursive=False)¶ 返回一个新的
CTE
或公共表表达式实例。公用表表达式是一种SQL标准,通过使用一个名为“WITH”的子句,SELECT语句可以使用与主语句一起指定的次要语句。有关UNION的特殊语义也可用于允许“递归”查询,其中SELECT语句可以在先前已选择的一组行上进行绘制。
CTE也可以应用于DML构造对某些数据库的UPDATE,INSERT和DELETE,与RETURNING一起作为CTE行的来源以及CTE行的使用者。
SQLAlchemy将
CTE
对象检测为与Alias
对象类似的对象,作为要传递到语句的FROM子句的特殊元素以及顶部的WITH子句的声明。在版本1.1中进行了更改:添加了对CTE,CTE添加到UPDATE / INSERT / DELETE的UPDATE / INSERT / DELETE的支持。
参数: 以下示例包含两篇来自Postgresql的文档,位于http://www.postgresql.org/docs/current/static/queries-with.html以及其他示例。
例1,非递归:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select([ orders.c.region, func.sum(orders.c.amount).label('total_sales') ]).group_by(orders.c.region).cte("regional_sales") top_regions = select([regional_sales.c.region]).\ where( regional_sales.c.total_sales > select([ func.sum(regional_sales.c.total_sales)/10 ]) ).cte("top_regions") statement = select([ orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ]).where(orders.c.region.in_( select([top_regions.c.region]) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
例2,与RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select([ parts.c.sub_part, parts.c.part, parts.c.quantity]).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select([ parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ]). where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select([ included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ]).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
例3,使用带CTE的UPDATE和INSERT的upsert:
orders = table( 'orders', column('region'), column('amount'), column('product'), column('quantity') ) upsert = ( orders.update() .where(orders.c.region == 'Region1') .values(amount=1.0, product='Product1', quantity=1) .returning(*(orders.c._all_columns)).cte('upsert')) insert = orders.insert().from_select( orders.c.keys(), select([ literal('Region1'), literal(1.0), literal('Product1'), literal(1) ).where(exists(upsert.select())) ) connection.execute(insert)
也可以看看
orm.query.Query.cte()
-HasCTE.cte()
的ORM版本。
描述 T0> ¶ T1>
- 从
description
继承FromClause
这个FromClause的简要描述。
主要用于错误消息格式。
执行 tt> ( * multiparams,** params ) T5>
- 继承自
execute()
方法Executable
编译并执行
Executable
。
execution_options T0> ( T1> **千瓦 T2> ) T3> ¶ T4>
- 继承自
execution_options()
方法 tt>Executable
为执行期间生效的语句设置非SQL选项。
执行选项可以在每个语句或每个
Connection
的基础上设置。此外,Engine
和ORMQuery
对象提供对执行选项的访问,而这些执行选项在连接时进行配置。execution_options()
方法是生成的。返回此语句的新实例,其中包含以下选项:statement = select([table.c.x, table.c.y]) statement = statement.execution_options(autocommit=True)
请注意,只有一部分可能的执行选项可以应用于语句 - 这些选项包括“autocommit”和“stream_results”,但不包括“isolation_level”或“compiled_cache”。有关可能的选项的完整列表,请参阅
Connection.execution_options()
。
foreign_keys T0> ¶ T1>
- 继承自
foreign_keys
属性FromClause
返回FromClause引用的ForeignKey对象的集合。
get_children T0> ( T1> ** kwargs T2> ) T3> ¶ T4>
- inherited from the
get_children()
method ofClauseElement
返回这个
ClauseElement
的直接子元素。这用于访问遍历。
** kwargs可能包含更改返回的集合的标志,例如为了减少更大的遍历而返回项目的子集,或者从不同的上下文返回子项目(例如模式级集合而不是子句-水平)。
is_derived_from T0> ( T1> fromclause T2> ) T3> ¶ T4>
- 继承自
is_derived_from()
方法FromClause
如果FromClause从给定的FromClause中“派生”,则返回True。
一个例子是从表中派生的表的别名。
-
join
(right, onclause=None, isouter=False, full=False)¶ - inherited from the
join()
method ofFromClause
从
FromClause
返回Join
到另一个FromClause
。例如。:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select([user_table]).select_from(j)
会沿着以下几行发出SQL:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
- 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。意味着
FromClause.join.isouter
。版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
标签 T0> ( T1> 名称 T2> ) T3> ¶ T4>
- 继承自
label()
方法SelectBase
返回这个可选择的“标量”表示,嵌入为带有标签的子查询。
也可以看看
横向 T0> ( T1> 名=无 T2> ) T3> ¶ T4>
- 从
lateral()
方法继承FromClause
返回这个
FromClause
的LATERAL别名。返回值是由顶层
lateral()
函数提供的Lateral
结构。版本1.1中的新功能
也可以看看
LATERAL correlation - overview of usage.
外连接
( 右,onclause =无,full = False ) T5> ¶ T6>- 从
outerjoin()
方法继承FromClause
Return a
Join
from thisFromClause
to anotherFromClause
, with the “isouter” flag set to True.例如。:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
以上相当于:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
参数: - 正确 ¶ - 连接的右侧;这是任何
FromClause
对象,如Table
对象,也可以是可选择兼容的对象,例如ORM映射类。 - onclause¶ – a SQL expression representing the ON clause of the join. 如果留在
None
,则FromClause.join()
将尝试基于外键关系来连接两个表。 - 完整 ¶ -
如果为True,则渲染一个FULL OUTER JOIN,而不是LEFT OUTER JOIN。
版本1.1中的新功能
- 正确 ¶ - 连接的右侧;这是任何
params
( * optionaldict,** kwargs ) T5>- inherited from the
params()
method ofClauseElement
返回带有
bindparam()
元素的副本。返回此ClauseElement的一个副本,其中
bindparam()
元素替换为从给定字典中取得的值:>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
primary_key T0> ¶ T1>
- 继承自
primary_key
属性FromClause
返回构成此FromClause主键的Column对象的集合。
-
replace_selectable
(old, alias)¶ - 继承自
replace_selectable()
方法FromClause
用给定的Alias对象替换所有出现的FromClause'old',并返回这个
FromClause
的副本。
标量
( * multiparams,** params ) T5>- inherited from the
scalar()
method ofExecutable
编译并执行此
Executable
,返回结果的标量表示。
选择
( whereclause = None,** params ) / T5>- inherited from the
select()
method ofFromClause
返回这个
FromClause
的SELECT。也可以看看
select()
- general purpose method which allows for arbitrary column lists.
self_group T0> ( T1> 针对=无 T2> ) T3> ¶ T4>
- inherited from the
self_group()
method ofClauseElement
对这个
ClauseElement
应用“分组”。子类重写此方法以返回“分组”结构,即括号。In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()
constructs when placed into the FROM clause of anotherselect()
. (请注意,通常应使用Select.alias()
方法创建子查询,因为许多平台需要命名嵌套的SELECT语句)。由于表达式组合在一起,所以
self_group()
的应用程序是自动的 - 最终用户代码不需要直接使用此方法。Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)
- AND takes precedence over OR.ClauseElement
的baseself_group()
方法仅返回self。
-
tablesample
(sampling, name=None, seed=None)¶ - inherited from the
tablesample()
method ofFromClause
返回此
FromClause
的TABLESAMPLE别名。返回值是顶级
tablesample()
函数也提供的TableSample
结构。版本1.1中的新功能
也可以看看
tablesample()
- 使用指南和参数
unique_params
( * optionaldict,** kwargs ) T5>- inherited from the
unique_params()
method ofClauseElement
返回带有
bindparam()
元素的副本。与
params()
功能相同,除了将unique = True添加到受影响的绑定参数以便可以使用多个语句。
-