SQLAlchemy 1.1文档
ORM内部¶
此处列出了其他章节未涉及的关键ORM结构。
- class
sqlalchemy.orm.state.
AttributeState
(state, key)¶ 提供与特定映射对象上的特定属性相对应的检查界面。
AttributeState
对象通过特定InstanceState
的InstanceState.attrs
集合进行访问:from sqlalchemy import inspect insp = inspect(some_mapped_object) attr_state = insp.attrs.some_attribute
load_history T0> ( T1> ) T2> ¶ T3>
通过
History
界面返回此属性的当前预冲刷更改历史记录。如果该属性的值被卸载,则此方法将发送加载器可调用对象。
版本0.9.0中的新功能
loaded_value T0> ¶ T1>
从数据库加载的此属性的当前值。
如果该值尚未加载,或者没有出现在对象的字典中,则返回NO_VALUE。
值 T0> ¶ T1>
返回此属性的值。
这个操作等同于直接或通过
getattr()
访问对象的属性,并且如果需要的话将触发任何挂起的加载器可调用。
- class
sqlalchemy.orm.util。
CascadeOptions
¶ Bases:
__builtin__.frozenset
跟踪发送给relationship()。cascade的选项
- 类 T0>
sqlalchemy.orm.instrumentation。 T1>
ClassManager T2> ( T3> 类_ T4> )< / T5> ¶ T6>
基础:
__builtin__.dict
跟踪课堂级别的状态信息。
__文件__ T0> ¶ T1>
- inherited from the
__le__
attribute ofdict
x .__ le __(y)<==> x <= y
__ LT __ T0> ¶ T1>
- inherited from the
__lt__
attribute ofdict
x.__lt__(y) <==> x
__ NE __ T0> ¶ T1>
- inherited from the
__ne__
attribute ofdict
x .__ ne __(y)<==> x!= y
清除
( )→无。删除D. ¶中的所有项目- inherited from the
clear()
method ofdict
拷贝
( )→D 的浅拷贝- inherited from the
copy()
method ofdict
处置 T0> ( T1> ) T2> ¶ T3>
将这位经理与同班同学分离。
-
fromkeys
(S[, v]) → New dict with keys from S and values equal to v.¶ - inherited from the
fromkeys()
method ofdict
v默认为None。
-
get
(k[, d]) → D[k] if k in D, else d. d defaults to None.¶ - 继承自
get()
方法dict
-
has_key
(k) → True if D has a key k, else False¶ - 继承自
has_key()
方法dict
has_parent
( state,key,optimistic = False T5> ¶ T6>去做
项
( )→D(键,值)对的列表,作为2元组¶- 继承自
items()
方法dict
-
iteritems
() → an iterator over the (key, value) items of D¶ - inherited from the
iteritems()
method ofdict
-
iterkeys
() → an iterator over the keys of D¶ - inherited from the
iterkeys()
method ofdict
-
itervalues
() → an iterator over the values of D¶ - inherited from the
itervalues()
method ofdict
键
( )→D键列表¶- inherited from the
keys()
method ofdict
管理 T0> ( T1> ) T2> ¶ T3>
将此实例标记为其类的管理器。
original_init T0> ¶ T1>
x .__ init __(...)初始化x;请参阅帮助(类型(x))进行签名
-
pop
(k[, d]) → v, remove specified key and return the corresponding value.¶ - 继承自
pop()
方法dict
如果未找到密钥,则在返回时返回d,否则引发KeyError
popitem
( )→(k,v),移除并返回一些(key,value)对作为¶ t3 >- 继承自
popitem()
方法dict
2元组;但如果D为空则引发KeyError。
-
setdefault
(k[, d]) → D.get(k,d), also set D[k]=d if k not in D¶ - 继承自
setdefault()
方法dict
state_getter T0> ( T1> ) T2> ¶ T3>
返回(实例) - > InstanceState可调用。
如果不能找到实例的InstanceState,“state getter”callables应该引发KeyError或AttributeError。
注销 T0> ( T1> ) T2> ¶ T3>
删除由该ClassManager建立的所有工具。
-
update
([E, ]**F) → None. 从dict / iterable E和F更新D。¶ - inherited from the
update()
method ofdict
如果E存在且具有.keys()方法,则:对于E中的k:D [k] = E [k]如果E存在并且缺少.keys()方法,则:for(k,v)in E: D [k] = v在任一情况下,这后面是:对于F中的k:D [k] = F [k]
值
( )→D值列表¶- 继承自
values()
方法dict
-
viewitems
() → a set-like object providing a view on D's items¶ - inherited from the
viewitems()
method ofdict
-
viewkeys
() → a set-like object providing a view on D's keys¶ - inherited from the
viewkeys()
method ofdict
viewvalues tt> ( )→提供D值视图的对象¶
- inherited from the
viewvalues()
method ofdict
- class
sqlalchemy.orm.properties.
ColumnProperty
(*columns, **kwargs)¶ 基础:
sqlalchemy.orm.interfaces.StrategizedProperty
描述对应于表列的对象属性。
公共构造函数是
orm.column_property()
函数。- class
比较器
( prop,parentmapper,adapt_to_entity = T5> ) T6> ¶ T7> 基础:
sqlalchemy.util.langhelpers.MemoizedSlots
,sqlalchemy.orm.interfaces.PropComparator
为
ColumnProperty
属性生成布尔值,比较和其他运算符。有关简要概述,请参阅
PropComparator
的文档。也可以看看:
Redefining and Creating New Operators
__当量__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
实施
==
运算符。在列上下文中,生成子句
a = b
。If the target isNone
, producesa IS NULL
.
__文件__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__le__()
method ofColumnOperators
实现
<=
运算符。在列上下文中,生成子句
a <= b
。
__ LT __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__lt__()
method ofColumnOperators
实现
<
运算符。在列上下文中,生成子句
a b
。
__ NE __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__ne__()
method ofColumnOperators
实现
!=
运算符。在列上下文中,生成子句
a != b
。If the target isNone
, producesa IS NOT NULL
.
adapt_to_entity T0> ( T1> adapt_to_entity T2> ) T3> ¶ T4>
- 继承自
adapt_to_entity()
方法PropComparator
返回此PropComparator的副本,它将使用给定的
AliasedInsp
生成相应的表达式。
适配器 T0> ¶ T1>
- 继承自
adapter
属性的PropComparator
生成一个可调用的列,适应列表达式以适应该比较器的别名版本。
任何
( criterion = None,** kwargs ) / T5>- inherited from the
any()
method ofPropComparator
如果此集合包含符合给定条件的任何成员,则返回true。
any()
的通常实现是RelationshipProperty.Comparator.any()
。参数:
任何_ T0> ( T1> ) T2> ¶ T3>
- inherited from the
any_()
method ofColumnOperators
针对父对象生成
any_()
子句。版本1.1中的新功能
ASC T0> ( T1> ) T2> ¶ T3>
- 继承自
asc()
ColumnOperators
针对父对象生成一个
asc()
子句。
( cleft,cright,symmetric = False T5> ¶ T6>
- inherited from the
between()
method ofColumnOperators
在()子句之间针对父对象生成
between()
整理 T0> ( T1> 整理 T2> ) T3> ¶ T4>
- inherited from the
collate()
method ofColumnOperators
根据给定的排序字符串,针对父对象生成一个
collate()
子句。
的concat T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现'concat'操作符。
在列上下文中,生成子句
a || b
,或者使用concat()
运算符在MySQL上。
包含
( 其他,** kwargs ) >- inherited from the
contains()
method ofColumnOperators
实现'包含'运算符。
在列上下文中,生成子句
LIKE '%&lt; other&gt;%'
不同 T0> ( T1> ) T2> ¶ T3>
- inherited from the
distinct()
method ofColumnOperators
针对父对象生成一个
distinct()
子句。
endswith
( 其他,** kwargs ) t5 >- inherited from the
endswith()
method ofColumnOperators
实现'endswith'操作符。
在列上下文中,生成子句
LIKE '%&lt; other&gt;
有
( criterion = None,** kwargs ) / T5>- inherited from the
has()
method ofPropComparator
如果此元素引用符合给定条件的成员,则返回true。
The usual implementation of
has()
isRelationshipProperty.Comparator.has()
.参数:
ilike
( 其他,escape =无 ) t5 >- inherited from the
ilike()
method ofColumnOperators
实现
ilike
运算符。在列上下文中,生成子句
a ILIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.ilike("%foobar%"))
参数:
在_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
in_()
method ofColumnOperators
在运算符中实现
in
在列上下文中,生成子句
a IN 其他
。“other”可以是列表达式的元组/列表,或者是select()
结构。
是_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
is_()
method ofColumnOperators
实现
IS
运算符。通常,当与
None
的值进行比较时,会自动生成IS
,这会解析为NULL
。但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS
。New in version 0.7.9.
is_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
is_distinct_from()
方法 tt>ColumnOperators
实现
IS DISTINCT FROM
运算符。在大多数平台上呈现“一个IS DISTINCT FROM b”;在一些如SQLite可能会呈现“一个不是b”。
版本1.1中的新功能
IsNot运算 T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
isnot()
method ofColumnOperators
实现
IS NOT
运算符。Normally,
IS NOT
is generated automatically when comparing to a value ofNone
, which resolves toNULL
. 但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS NOT
。New in version 0.7.9.
isnot_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现
IS NOT DISTINCT FROM
运算符。在大多数平台上呈现“不是从BIND DISTINCT FROM b”;在某些例如SQLite上可能会呈现“a IS b”。
版本1.1中的新功能
像
( 其他,escape =无 ) t5 >- inherited from the
like()
method ofColumnOperators
像运算符一样实现
like
在列上下文中,生成子句
a LIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.like("%foobar%"))
参数:
匹配
( 其他,** kwargs ) t5 >- 继承自
match()
方法 tt>ColumnOperators
实现数据库特定的“匹配”运算符。
match()
attempts to resolve to a MATCH-like function or operator provided by the backend. 例子包括:- Postgresql - 呈现
x @@ to_tsquery(y)
- MySQL - renders
MATCH (x) AGAINST (y IN BOOLEAN MODE)
- Oracle - 呈现
CONTAINS(x, y)
- 其他后端可能会提供特殊的实现。
- 没有任何特殊实现的后端会将操作符发送为“MATCH”。例如,这与SQlite兼容。
- Postgresql - 呈现
-
notilike
(other, escape=None)¶ - inherited from the
notilike()
method ofColumnOperators
执行
NOT ILIKE
运算符。这相当于对
ColumnOperators.ilike()
使用否定,即~x.ilike(y)
。0.8版本中的新功能
notin _ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
notin_()
方法ColumnOperators
执行
NOT IN
运算符。这相当于对
ColumnOperators.in_()
,即~x.in_(y)
使用否定。0.8版本中的新功能
notlike
( 其他,转义=无 ) t5 >- inherited from the
notlike()
method ofColumnOperators
执行
NOT LIKE
运算符。这相当于对
ColumnOperators.like()
,即~x.like(y)
使用否定。0.8版本中的新功能
nullsfirst T0> ( T1> ) T2> ¶ T3>
- inherited from the
nullsfirst()
method ofColumnOperators
针对父对象生成
nullsfirst()
子句。
nullslast T0> ( T1> ) T2> ¶ T3>
针对父对象生成一个
nullslast()
子句。
of_type T0> ( T1> 类_ T2> ) T3> ¶ T4>
- inherited from the
of_type()
method ofPropComparator
用多态子类重新定义这个对象。
返回可以从中评估更多标准的新PropComparator。
例如。:
query.join(Company.employees.of_type(Engineer)).\ filter(Engineer.name=='foo')
参数: class_¶ – a class or mapper indicating that criterion will be against this specific subclass.
op
( opstring,precedence = 0,is_comparison = False ) T5> ¶ T6>产生通用的操作员功能。
例如。:
somecolumn.op("*")(5)
生产:
somecolumn * 5
该函数也可用于使按位运算符明确。例如:
somecolumn.op('&')(0xff)
是
somecolumn
中的值的按位与。参数: - operator¶ – a string which will be output as the infix operator between this element and the expression passed to the generated function.
- 优先顺序 ¶ -
当对表达式加括号时,优先级适用于运算符。较低的数字将使表达式在针对具有较高优先级的另一个运算符应用时加括号。除了逗号(
,
)和AS
运算符以外,0
的默认值低于所有运算符。100的值将会高于或等于所有操作员,-100将低于或等于所有操作员。New in version 0.8: - added the ‘precedence’ argument.
- is_comparison ¶ -
如果为True,那么该运算符将被视为“比较”运算符,即,其计算结果为boolean true / false值,如
==
,>
等。应该设置此标志,以便ORM关系可以确定运算符在自定义连接条件中使用时是比较运算符。版本0.9.2新增: - 添加了
Operators.op.is_comparison
标志。
startswith
( 其他,** kwargs ) ¶ t5 >- inherited from the
startswith()
method ofColumnOperators
实现
startwith
运算符。在列上下文中,生成子句
LIKE '&lt; other&gt;%'
ColumnProperty。
__ init __
( *列,** kwargs / T5> ¶ T6>构建一个新的
ColumnProperty
对象。这个构造函数被镜像为公共API函数;有关完整的用法和参数说明,请参阅
column_property()
。
ColumnProperty
cascade_iterator
( type,状态,visited_instances = / t5>,halt_on = None ) ¶- inherited from the
cascade_iterator()
method ofMapperProperty
从MapperProperty开始,迭代与特定“级联”相关的实例。
返回一个iterator3-tuples(实例,映射器,状态)。
请注意,在调用cascade_iterator之前,此MapperProperty上的'cascade'集合将首先针对给定类型进行检查。
此方法通常仅适用于RelationshipProperty。
COLUMNPROPERTY。 T0>
class_attribute T1> ¶ T2>
- 继承自
MapperProperty
的class_attribute
属性返回与此
MapperProperty
对应的类绑定描述符。这基本上是一个
getattr()
调用:return getattr(self.parent.class_, self.key)
即如果这个
MapperProperty
被命名为addresses
,并且它映射到的类是User
,那么这个序列是可能的:>>> from sqlalchemy import inspect >>> mapper = inspect(User) >>> addresses_property = mapper.attrs.addresses >>> addresses_property.class_attribute is User.addresses True >>> User.addresses.property is addresses_property True
COLUMNPROPERTY。 T0>
表达 T1> ¶ T2>
返回此ColumnProperty的主列或表达式。
ColumnProperty。
extension_type
=符号('NOT_EXTENSION') ¶
COLUMNPROPERTY。 T0>
初始化 T1> ( T2> ) T3> ¶ T4>
- inherited from the
init()
method ofMapperProperty
在创建所有映射器以调用映射器之间的关系并执行其他映射器创建初始化步骤后调用。
-
ColumnProperty.
set_parent
(parent, init)¶ - inherited from the
set_parent()
method ofMapperProperty
设置引用此MapperProperty的父映射器。
当映射器第一次知道时,此方法被一些子类覆盖以执行额外的设置。
- class
- class
sqlalchemy.orm.properties。
ComparableProperty
( comparator_factory,=无,doc =无,info =无 ) ¶ 基础:
sqlalchemy.orm.descriptor_props.DescriptorProperty
在查询表达式中使用Python属性。
-
__init__
(comparator_factory, descriptor=None, doc=None, info=None)¶ 构建一个新的
ComparableProperty
对象。这个构造函数被镜像为公共API函数;有关完整的用法和参数说明,请参阅
comparable_property()
。
-
- class
sqlalchemy.orm.descriptor_props。
CompositeProperty
( class _,* attrs,** kwargs ) ¶ 基础:
sqlalchemy.orm.descriptor_props.DescriptorProperty
定义一个“复合”映射属性,将一组列作为一个属性表示。
CompositeProperty
是使用composite()
函数构造的。- class
比较器
( prop,parentmapper,adapt_to_entity = T5> ) T6> ¶ T7> 基础:
sqlalchemy.orm.interfaces.PropComparator
为
CompositeProperty
属性生成布尔型,比较和其他运算符。请参阅Redefining Comparison Operations for Composites中的示例以了解用法概述以及
PropComparator
的文档。也可以看看:
CompositeProperty。
__ init __
( class _,* attrs,** kwargs T5> ) T6> ¶ T7>构建一个新的
CompositeProperty
对象。这个构造函数被镜像为公共API函数;有关完整的用法和参数说明,请参阅
composite()
。
CompositeProperty。 T0>
do_init T1> ( T2> ) T3> ¶ T4>
初始化发生在
CompositeProperty
与其父映射器关联之后。
-
CompositeProperty.
get_history
(state, dict_, passive=symbol('PASSIVE_OFF'))¶ 为使用attributes.get_history()的用户级代码提供。
- class
- class
sqlalchemy.orm.attributes。
Event
( attribute_impl,op T5> ) T6> ¶ T7> 一个令牌在一系列属性事件中传播。
作为事件来源的指标,同时也提供了控制属性操作链中传播的手段。
The
Event
object is sent as theinitiator
argument when dealing with theAttributeEvents.append()
,AttributeEvents.set()
, andAttributeEvents.remove()
events.Event
对象当前由backref事件处理程序解释,并用于控制跨两个相互依赖的属性的操作传播。版本0.9.0中的新功能
变量:
- class
sqlalchemy.orm.identity。
IdentityMap
¶ check_modified T0> ( T1> ) T2> ¶ T3>
如果任何InstanceStates存在已被标记为“已修改”,则返回True。
- class
sqlalchemy.orm.base。
InspectionAttr
¶ 基类应用于所有可由
inspect()
函数返回的ORM对象。这里定义的属性允许使用简单的布尔检查来测试返回对象的基本事实。
虽然这里的布尔检查与使用Python isinstance()函数基本相同,但可以在不需要导入所有这些类的情况下使用此处的标记,还可以使SQLAlchemy类系统更改,同时保持标记处于完整状态为了向前兼容。
extension_type
=符号('NOT_EXTENSION') ¶扩展类型,如果有的话。默认为
interfaces.NOT_EXTENSION
0.8.0版本中的新功能
is_aliased_class
= False ¶如果此对象是
AliasedClass
的实例,则为true。
is_attribute
= False ¶如果此对象是Python descriptor,则为真。
这可以指许多类型之一。通常是一个
QueryableAttribute
,它代表MapperProperty
处理属性事件。但也可以是扩展类型,如AssociationProxy
或hybrid_property
。InspectionAttr.extension_type
将引用标识特定子类型的常量。
is_clause_element
= False ¶如果此对象是
ClauseElement
的实例,则为true。
is_instance
= False ¶如果此对象是
InstanceState
的实例,则为true。
is_property
= False ¶如果此对象是
MapperProperty
的实例,则为true。
is_selectable
= False ¶如果此对象是
Selectable
的实例,则返回True。
- class
sqlalchemy.orm.base。
InspectionAttrInfo
¶ 基础:
sqlalchemy.orm.base.InspectionAttr
将
.info
属性添加到InspectionAttr
。The rationale for
InspectionAttr
vs.InspectionAttrInfo
is that the former is compatible as a mixin for classes that specify__slots__
; this is essentially an implementation artifact.信息 T0> ¶ T1>
信息字典与对象关联,允许用户定义的数据与这个
InspectionAttr
关联。字典在第一次访问时生成。Alternatively, it can be specified as a constructor argument to the
column_property()
,relationship()
, orcomposite()
functions.0.8版新增功能:增加了对所有
MapperProperty
子类的.info支持。版本1.0.0更改:
MapperProperty.info
也可以通过InspectionAttrInfo.info
属性在扩展类型上使用,以便它可以应用于更广泛的ORM和扩展结构。
- class
sqlalchemy.orm.state。
InstanceState
( obj,manager T5> ) T6> ¶ T7> 基础:
sqlalchemy.orm.base.InspectionAttr
跟踪实例级别的状态信息。
InstanceState
是SQLAlchemy ORM使用的关键对象,用于跟踪对象的状态;它是在实例化对象的时刻创建的,通常是SQLAlchemy应用于该类的__init__()
方法的结果instrumentation。InstanceState
is also a semi-public object, available for runtime inspection as to the state of a mapped instance, including information such as its current status within a particularSession
and details about data on individual attributes. 为了获取InstanceState
对象的公共API是使用inspect()
系统:>>> from sqlalchemy import inspect >>> insp = inspect(some_mapped_object)
ATTRS T0> ¶ T1>
返回表示映射对象上每个属性的名称空间,包括其当前值和历史记录。
返回的对象是
AttributeState
的一个实例。该对象允许检查属性中的当前数据以及自上次刷新后的属性历史记录。
callables
=() ¶每个状态加载器可调用的名称空间可以关联。
在SQLAlchemy 1.0中,这仅用于通过查询选项设置的懒加载器/延迟加载器。
以前,通过在此字典中存储指向InstanceState本身的链接,可调用符号也用于指示过期的属性。此角色现在由expired_attributes集处理。
删除 T0> ¶ T1>
如果对象deleted,则返回true。
处于删除状态的对象保证不在其父节点
Session
的Session.identity_map
内;但是如果会话的事务回滚,对象将被恢复到持久状态和身份映射。注意
InstanceState.deleted
属性是指在“持久”和“分离”状态之间发生的对象的特定状态;一旦对象detached,InstanceState.deleted
属性不再返回True;为了检测状态是否被删除,无论对象是否与Session
关联,请使用InstanceState.was_deleted
访问器。
字典 T0> ¶ T1>
返回对象使用的实例字典。
在正常情况下,除非已配置备用仪器系统,否则这总是与映射对象的
__dict__
属性同义。在实际对象已被垃圾收集的情况下,这个访问器返回一个空白字典。
expired_attributes
=无 ¶假设没有待处理的更改,将由管理器的延迟标量加载器加载的'过期'密钥集。
另请参阅发生刷新操作时与此set相交的
unmodified
集合。
has_identity T0> ¶ T1>
如果此对象具有标识关键字,则返回
True
。这应该始终与表达式
state.persistent 或 state.detached
具有相同的值。
身份 T0> ¶ T1>
返回映射对象的映射标识。这是ORM持久保存的主键标识,它总是可以直接传递给
Query.get()
。如果对象没有主键标识,则返回
None
。
identity_key T0> ¶ T1>
返回映射对象的身份密钥。
这是用于在
Session.identity_map
映射中定位对象的关键。它包含由identity
返回的身份。
对象 T0> ¶ T1>
返回由
InstanceState
表示的映射对象。
持久 T0> ¶ T1>
如果对象是persistent,则返回true。
处于持久状态的对象保证位于其父节点
Session
的Session.identity_map
内。在版本1.1中更改:对于在刷新中“删除”的对象,
InstanceState.persistent
访问器不再返回True。使用InstanceState.deleted
访问器来检测此状态。这允许“持久”状态保证身份映射中的成员身份。
会话 T0> ¶ T1>
如果没有可用的,则返回此实例的拥有
Session
,或返回None
。请注意,这里的结果在某些情况下可能与
obj 在 会话中的不同 T1>;一个被删除的对象会在 session中报告为不是
只有在交易完成后,物品才能在正常情况下完全分离。,但是如果事务仍在进行中,该属性仍然会引用该会话。
卸载 T0> ¶ T1>
返回没有加载值的一组键。
这包括过期的属性和从未被填充或修改过的任何其他属性。
未修饰 T0> ¶ T1>
返回没有未提交更改的密钥集
unmodified_intersection T0> ( T1> 键 T2> ) T3> ¶ T4>
返回self.unmodified.intersection(键)。
was_deleted T0> ¶ T1>
如果此对象处于或以前处于“已删除”状态且尚未恢复为持续状态,则返回True。
一旦对象在flush中被删除,该标志返回True。当显式地或通过事务提交从会话中清除对象并进入“分离”状态时,该标志将继续报告为真。
版本1.1中的新增功能: - 添加了
orm.util.was_deleted()
的本地方法形式。
- class
sqlalchemy.orm.attributes.
InstrumentedAttribute
(class_, key, impl=None, comparator=None, parententity=None, of_type=None)¶ 基础:
sqlalchemy.orm.attributes.QueryableAttribute
添加基本descriptor方法的类绑定插装属性。
有关大多数功能的描述,请参阅
QueryableAttribute
。__删除__ T0> ( T1> 实例 T2> ) T3> ¶ T4>
-
__get__
(instance, owner)¶
-
__set__
(instance, value)¶
sqlalchemy.orm.interfaces。
MANYTOONE
= symbol('MANYTOONE') ¶指示
relationship()
的多对一方向。此符号通常由内部使用,但可能会暴露在某些API功能中。
sqlalchemy.orm.interfaces。
MANYTOMANY
= symbol('MANYTOMANY') ¶指示
relationship()
的多对多方向。此符号通常由内部使用,但可能会暴露在某些API功能中。
- class
sqlalchemy.orm.interfaces。
MapperProperty
¶ 基础:
sqlalchemy.orm.base._MappedAttribute
,sqlalchemy.orm.base.InspectionAttr
,sqlalchemy.util.langhelpers.MemoizedSlots
表示由
Mapper
映射的特定类属性。最常见的
MapperProperty
是映射的Column
,它在映射中表示为ColumnProperty
的实例,并且对另一个类的引用由relationship()
生成,在映射中表示为RelationshipProperty
的实例。信息 T0> ¶ T1>
信息字典与对象关联,允许用户定义的数据与这个
InspectionAttr
关联。字典在第一次访问时生成。Alternatively, it can be specified as a constructor argument to the
column_property()
,relationship()
, orcomposite()
functions.0.8版新增功能:增加了对所有
MapperProperty
子类的.info支持。版本1.0.0中已更改:
InspectionAttr.info
从MapperProperty
移动,以便它可以应用于更广泛的ORM和扩展结构。
cascade
= frozenset([]) ¶'cascade'属性名称的集合。
在调用“cascade_iterator”方法之前检查此集合。
该集合通常仅适用于RelationshipProperty。
-
cascade_iterator
(type_, state, visited_instances=None, halt_on=None)¶ 从MapperProperty开始,迭代与特定“级联”相关的实例。
返回一个iterator3-tuples(实例,映射器,状态)。
请注意,在调用cascade_iterator之前,此MapperProperty上的'cascade'集合将首先针对给定类型进行检查。
此方法通常仅适用于RelationshipProperty。
class_attribute T0> ¶ T1>
返回与此
MapperProperty
对应的类绑定描述符。这基本上是一个
getattr()
调用:return getattr(self.parent.class_, self.key)
即如果这个
MapperProperty
被命名为addresses
,并且它映射到的类是User
,那么这个序列是可能的:>>> from sqlalchemy import inspect >>> mapper = inspect(User) >>> addresses_property = mapper.attrs.addresses >>> addresses_property.class_attribute is User.addresses True >>> User.addresses.property is addresses_property True
-
create_row_processor
(context, path, mapper, result, adapter, populators)¶ 生成行处理函数并追加到给定的填充列表集。
do_init T0> ( T1> ) T2> ¶ T3>
执行子类特定的初始化后映射器创建步骤。
这是一个由
MapperProperty
对象的init()方法调用的模板方法。
初始化 T0> ( T1> ) T2> ¶ T3>
在创建所有映射器以调用映射器之间的关系并执行其他映射器创建初始化步骤后调用。
instrument_class T0> ( T1> 映射器 T2> ) T3> ¶ T4>
Hook被Mapper调用到该属性,以启动由此MapperProperty管理的类属性的检测。
这里的MapperProperty通常会调用属性模块来设置InstrumentedAttribute。
这一步是设置InstrumentedAttribute的两个步骤中的第一步,并且在映射器设置过程的早期调用。
第二步通常是init_class_attribute步骤,通过post_instrument_class()钩子从StrategizedProperty调用。这一步为InstrumentedAttribute(特别是“impl”)分配额外的状态,这个状态在MapperProperty确定了它需要做什么样的持久性管理(例如标量,对象,集合等)之后确定。
is_property
= True ¶InspectionAttr接口的一部分;声明这个对象是一个映射器属性。
-
merge
(session, source_state, source_dict, dest_state, dest_dict, load, _recursive, _resolve_conflict_map)¶ 合并由
MapperProperty
表示的属性从源到目标对象。
post_instrument_class T0> ( T1> 映射器 T2> ) T3> ¶ T4>
执行init()完成后需要进行的仪器调整。
给定的Mapper是调用操作的Mapper,在继承场景中可能不是与self.parent相同的映射器;但是,Mapper将始终至少成为self.parent的子映射器。
该方法通常由StrategizedProperty使用,该方法将其委派给LoaderStrategy.init_class_attribute()以对类绑定的InstrumentedAttribute执行最终设置。
-
setup
(context, entity, path, adapter, **kwargs)¶ 由Query调用以构造SQL语句。
与目标映射器关联的每个MapperProperty处理查询上下文引用的语句,并根据需要添加列和/或标准。
sqlalchemy.orm.interfaces。
NOT_EXTENSION
=符号('NOT_EXTENSION') ¶指示不属于sqlalchemy.ext的
InspectionAttr
的符号。
sqlalchemy.orm.interfaces。
ONETOMANY
= symbol('ONETOMANY') ¶指示
relationship()
的一对多方向。此符号通常由内部使用,但可能会暴露在某些API功能中。
- class
sqlalchemy.orm.interfaces.
PropComparator
(prop, parentmapper, adapt_to_entity=None)¶ 基础:
sqlalchemy.sql.operators.ColumnOperators
为
MapperProperty
对象定义SQL运算符。SQLAlchemy允许在Core和ORM级别重新定义运算符。
PropComparator
is the base class of operator redefinition for ORM-level operations, including those ofColumnProperty
,RelationshipProperty
, andCompositeProperty
.注意
随着SQLAlchemy 0.7中引入的混合属性的出现以及SQLAlchemy 0.8中的核心级操作符重新定义,用户定义的
PropComparator
实例的用例非常罕见。请参阅Hybrid Attributes以及Redefining and Creating New Operators。可以创建
PropComparator
的用户定义的子类。内置的Python比较和数学运算符方法,如operators.ColumnOperators.__eq__()
,operators.ColumnOperators.__lt__()
和operators.ColumnOperators.__add__()
,可以被覆盖以提供新的操作员行为。自定义PropComparator
通过参数comparator_factory
传递给MapperProperty
实例。在每种情况下,应使用PropComparator
的适当子类:# definition of custom PropComparator subclasses from sqlalchemy.orm.properties import \ ColumnProperty,\ CompositeProperty,\ RelationshipProperty class MyColumnComparator(ColumnProperty.Comparator): def __eq__(self, other): return self.__clause_element__() == other class MyRelationshipComparator(RelationshipProperty.Comparator): def any(self, expression): "define the 'any' operation" # ... class MyCompositeComparator(CompositeProperty.Comparator): def __gt__(self, other): "redefine the 'greater than' operation" return sql.and_(*[a>b for a, b in zip(self.__clause_element__().clauses, other.__composite_values__())]) # application of custom PropComparator subclasses from sqlalchemy.orm import column_property, relationship, composite from sqlalchemy import Column, String class SomeMappedClass(Base): some_column = column_property(Column("some_column", String), comparator_factory=MyColumnComparator) some_relationship = relationship(SomeOtherClass, comparator_factory=MyRelationshipComparator) some_composite = composite( Column("a", String), Column("b", String), comparator_factory=MyCompositeComparator )
请注意,对于列级别运算符重新定义,通常使用
TypeEngine.comparator_factory
属性在核心级别定义运算符更为简单。有关更多详细信息,请参阅Redefining and Creating New Operators。也可以看看:
RelationshipProperty.Comparator
Redefining and Creating New Operators
__当量__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
实施
==
运算符。在列上下文中,生成子句
a = b
。If the target isNone
, producesa IS NULL
.
__文件__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__le__()
method ofColumnOperators
实现
<=
运算符。在列上下文中,生成子句
a <= b
。
__ LT __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__lt__()
method ofColumnOperators
实现
<
运算符。在列上下文中,生成子句
a b
。
__ NE __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__ne__()
method ofColumnOperators
实现
!=
运算符。在列上下文中,生成子句
a != b
。If the target isNone
, producesa IS NOT NULL
.
adapt_to_entity T0> ( T1> adapt_to_entity T2> ) T3> ¶ T4>
返回此PropComparator的副本,它将使用给定的
AliasedInsp
生成相应的表达式。
适配器 T0> ¶ T1>
生成一个可调用的列,适应列表达式以适应该比较器的别名版本。
任何
( criterion = None,** kwargs ) / T5>如果此集合包含符合给定条件的任何成员,则返回true。
any()
的通常实现是RelationshipProperty.Comparator.any()
。参数:
任何_ T0> ( T1> ) T2> ¶ T3>
- inherited from the
any_()
method ofColumnOperators
针对父对象生成
any_()
子句。版本1.1中的新功能
ASC T0> ( T1> ) T2> ¶ T3>
- 继承自
asc()
ColumnOperators
针对父对象生成一个
asc()
子句。
( cleft,cright,symmetric = False T5> ¶ T6>
- inherited from the
between()
method ofColumnOperators
在()子句之间针对父对象生成
between()
整理 T0> ( T1> 整理 T2> ) T3> ¶ T4>
- inherited from the
collate()
method ofColumnOperators
根据给定的排序字符串,针对父对象生成一个
collate()
子句。
的concat T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现'concat'操作符。
在列上下文中,生成子句
a || b
,或者使用concat()
运算符在MySQL上。
包含
( 其他,** kwargs ) >- inherited from the
contains()
method ofColumnOperators
实现'包含'运算符。
在列上下文中,生成子句
LIKE '%&lt; other&gt;%'
不同 T0> ( T1> ) T2> ¶ T3>
- inherited from the
distinct()
method ofColumnOperators
针对父对象生成一个
distinct()
子句。
endswith
( 其他,** kwargs ) t5 >- inherited from the
endswith()
method ofColumnOperators
实现'endswith'操作符。
在列上下文中,生成子句
LIKE '%&lt; other&gt;
有
( criterion = None,** kwargs ) / T5>如果此元素引用符合给定条件的成员,则返回true。
The usual implementation of
has()
isRelationshipProperty.Comparator.has()
.参数:
ilike
( 其他,escape =无 ) t5 >- inherited from the
ilike()
method ofColumnOperators
实现
ilike
运算符。在列上下文中,生成子句
a ILIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.ilike("%foobar%"))
参数:
在_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
in_()
method ofColumnOperators
在运算符中实现
in
在列上下文中,生成子句
a IN 其他
。“other”可以是列表达式的元组/列表,或者是select()
结构。
是_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
is_()
method ofColumnOperators
实现
IS
运算符。通常,当与
None
的值进行比较时,会自动生成IS
,这会解析为NULL
。但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS
。New in version 0.7.9.
is_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
is_distinct_from()
方法 tt>ColumnOperators
实现
IS DISTINCT FROM
运算符。在大多数平台上呈现“一个IS DISTINCT FROM b”;在一些如SQLite可能会呈现“一个不是b”。
版本1.1中的新功能
IsNot运算 T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
isnot()
method ofColumnOperators
实现
IS NOT
运算符。Normally,
IS NOT
is generated automatically when comparing to a value ofNone
, which resolves toNULL
. 但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS NOT
。New in version 0.7.9.
isnot_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现
IS NOT DISTINCT FROM
运算符。在大多数平台上呈现“不是从BIND DISTINCT FROM b”;在某些例如SQLite上可能会呈现“a IS b”。
版本1.1中的新功能
像
( 其他,escape =无 ) t5 >- inherited from the
like()
method ofColumnOperators
像运算符一样实现
like
在列上下文中,生成子句
a LIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.like("%foobar%"))
参数:
匹配
( 其他,** kwargs ) t5 >- 继承自
match()
方法 tt>ColumnOperators
实现数据库特定的“匹配”运算符。
match()
attempts to resolve to a MATCH-like function or operator provided by the backend. 例子包括:- Postgresql - 呈现
x @@ to_tsquery(y)
- MySQL - renders
MATCH (x) AGAINST (y IN BOOLEAN MODE)
- Oracle - 呈现
CONTAINS(x, y)
- 其他后端可能会提供特殊的实现。
- 没有任何特殊实现的后端会将操作符发送为“MATCH”。例如,这与SQlite兼容。
- Postgresql - 呈现
-
notilike
(other, escape=None)¶ - inherited from the
notilike()
method ofColumnOperators
执行
NOT ILIKE
运算符。这相当于对
ColumnOperators.ilike()
使用否定,即~x.ilike(y)
。0.8版本中的新功能
notin _ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
notin_()
方法ColumnOperators
执行
NOT IN
运算符。这相当于对
ColumnOperators.in_()
,即~x.in_(y)
使用否定。0.8版本中的新功能
notlike
( 其他,转义=无 ) t5 >- inherited from the
notlike()
method ofColumnOperators
执行
NOT LIKE
运算符。这相当于对
ColumnOperators.like()
,即~x.like(y)
使用否定。0.8版本中的新功能
nullsfirst T0> ( T1> ) T2> ¶ T3>
- inherited from the
nullsfirst()
method ofColumnOperators
针对父对象生成
nullsfirst()
子句。
nullslast T0> ( T1> ) T2> ¶ T3>
针对父对象生成一个
nullslast()
子句。
of_type T0> ( T1> 类_ T2> ) T3> ¶ T4>
用多态子类重新定义这个对象。
返回可以从中评估更多标准的新PropComparator。
例如。:
query.join(Company.employees.of_type(Engineer)).\ filter(Engineer.name=='foo')
参数: class_¶ – a class or mapper indicating that criterion will be against this specific subclass.
op
( opstring,precedence = 0,is_comparison = False ) T5> ¶ T6>产生通用的操作员功能。
例如。:
somecolumn.op("*")(5)
生产:
somecolumn * 5
该函数也可用于使按位运算符明确。例如:
somecolumn.op('&')(0xff)
是
somecolumn
中的值的按位与。参数: - operator¶ – a string which will be output as the infix operator between this element and the expression passed to the generated function.
- 优先顺序 ¶ -
当对表达式加括号时,优先级适用于运算符。较低的数字将使表达式在针对具有较高优先级的另一个运算符应用时加括号。除了逗号(
,
)和AS
运算符以外,0
的默认值低于所有运算符。100的值将会高于或等于所有操作员,-100将低于或等于所有操作员。New in version 0.8: - added the ‘precedence’ argument.
- is_comparison ¶ -
如果为True,那么该运算符将被视为“比较”运算符,即,其计算结果为boolean true / false值,如
==
,>
等。应该设置此标志,以便ORM关系可以确定运算符在自定义连接条件中使用时是比较运算符。版本0.9.2新增: - 添加了
Operators.op.is_comparison
标志。
操作 tt> ( op,*其他,** kwargs / T5> ¶ T6>
操作一个参数。
这是最低级别的操作,缺省情况下会引发
NotImplementedError
。在子类上覆盖它可以使普通行为适用于所有操作。例如,覆盖
ColumnOperators
将func.lower()
应用于左侧和右侧:class MyComparator(ColumnOperators): def operate(self, op, other): return op(func.lower(self), func.lower(other))
参数:
reverse_operate
( op,其他,** kwargs T5> ¶ T6>- inherited from the
reverse_operate()
method ofOperators
对参数进行反向操作。
用法与
operate()
相同。
startswith
( 其他,** kwargs ) ¶ t5 >- inherited from the
startswith()
method ofColumnOperators
实现
startwith
运算符。在列上下文中,生成子句
LIKE '&lt; other&gt;%'
- class
sqlalchemy.orm.properties.
RelationshipProperty
(argument, secondary=None, primaryjoin=None, secondaryjoin=None, foreign_keys=None, uselist=None, order_by=False, backref=None, back_populates=None, post_update=False, cascade=False, extension=None, viewonly=False, lazy=True, collection_class=None, passive_deletes=False, passive_updates=True, remote_side=None, enable_typechecks=True, join_depth=None, comparator_factory=None, single_parent=False, innerjoin=False, distinct_target_key=None, doc=None, active_history=False, cascade_backrefs=True, load_on_pending=False, bake_queries=True, strategy_class=None, _local_remote_pairs=None, query_class=None, info=None)¶ 基础:
sqlalchemy.orm.interfaces.StrategizedProperty
描述保存与相关数据库表对应的单个项目或项目列表的对象属性。
公共构造函数是
orm.relationship()
函数。也可以看看:
- class
比较器
( prop,parentmapper,adapt_to_entity = t5>,of_type = None ) ¶ 基础:
sqlalchemy.orm.interfaces.PropComparator
为
RelationshipProperty
属性生成布尔型,比较型和其他运算符。有关ORM级别操作符定义的简要概述,请参阅
PropComparator
的文档。也可以看看:
Redefining and Creating New Operators
__当量__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
实施
==
运算符。在多对一的情况下,比如:
MyClass.some_prop == <some object>
这通常会产生一个条款,例如:
mytable.related_id == <some id>
其中
&lt; some id&gt;
是给定对象的主键。==
运算符提供了非多对一比较的部分功能:- 不支持与集合的比较。使用
contains()
。 - 与标量一对多比较,将生成一个子句,将父级中的目标列与给定目标进行比较。
- 与标量多对多相比,关联表的别名也将被渲染,形成查询主体的一部分的自然连接。这不适用于超出简单和比较连接的查询,例如那些使用OR的查询。使用显式连接,外连接或
has()
来进行更全面的非多对一标量成员测试。 - 在一对多或多对多的上下文中给出的与
None
的比较产生NOT EXISTS子句。
- 不支持与集合的比较。使用
-
__init__
(prop, parentmapper, adapt_to_entity=None, of_type=None)¶ RelationshipProperty.Comparator
的结构是ORM属性结构的内部结构。
__文件__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__le__()
method ofColumnOperators
实现
<=
运算符。在列上下文中,生成子句
a <= b
。
__ LT __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__lt__()
method ofColumnOperators
实现
<
运算符。在列上下文中,生成子句
a b
。
__ NE __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现
!=
运算符。在多对一的情况下,比如:
MyClass.some_prop != <some object>
这通常会产生一个条款,例如:
mytable.related_id != <some id>
其中
&lt; some id&gt;
是给定对象的主键。!=
运算符提供了非多对一比较的部分功能:- 不支持与集合的比较。将
contains()
与not_()
结合使用。 - 与标量一对多比较,将生成一个子句,将父级中的目标列与给定目标进行比较。
- 与标量多对多相比,关联表的别名也将被渲染,形成查询主体的一部分的自然连接。这不适用于超出简单和比较连接的查询,例如那些使用OR的查询。使用显式连接,外连接或
has()
与not_()
配合使用,以获得更全面的非多对一标量成员资格测试。 - 在一对多或多对多的上下文中给出的与
None
的比较产生了EXISTS子句。
- 不支持与集合的比较。将
适配器 T0> ¶ T1>
- 继承自
adapter
属性的PropComparator
生成一个可调用的列,适应列表达式以适应该比较器的别名版本。
任何
( criterion = None,** kwargs ) / T5>根据特定标准生成一个表达式,使用EXISTS测试集合。
表达式如下:
session.query(MyClass).filter( MyClass.somereference.any(SomeRelated.x==2) )
将产生如下查询:
SELECT * FROM my_table WHERE EXISTS (SELECT 1 FROM related WHERE related.my_id=my_table.id AND related.x=2)
由于
any()
使用相关的子查询,因此与大型目标表相比,其性能几乎不如使用连接的性能好。any()
is particularly useful for testing for empty collections:session.query(MyClass).filter( ~MyClass.somereference.any() )
会产生:
SELECT * FROM my_table WHERE NOT EXISTS (SELECT 1 FROM related WHERE related.my_id=my_table.id)
any()
is only valid for collections, i.e. arelationship()
that hasuselist=True
. 对于标量引用,请使用has()
。
任何_ T0> ( T1> ) T2> ¶ T3>
- inherited from the
any_()
method ofColumnOperators
针对父对象生成
any_()
子句。版本1.1中的新功能
ASC T0> ( T1> ) T2> ¶ T3>
- 继承自
asc()
ColumnOperators
针对父对象生成一个
asc()
子句。
( cleft,cright,symmetric = False T5> ¶ T6>
- inherited from the
between()
method ofColumnOperators
在()子句之间针对父对象生成
between()
整理 T0> ( T1> 整理 T2> ) T3> ¶ T4>
- inherited from the
collate()
method ofColumnOperators
根据给定的排序字符串,针对父对象生成一个
collate()
子句。
的concat T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现'concat'操作符。
在列上下文中,生成子句
a || b
,或者使用concat()
运算符在MySQL上。
包含
( 其他,** kwargs ) >返回一个简单的表达式,用于测试集合是否包含特定的项目。
contains()
仅对集合有效,即relationship()
用uselist=True
当在简单的一对多上下文中使用时,表达式如下所示:
MyClass.contains(other)
生成一个子句,如:
mytable.id == <some id>
Where
<some id>
is the value of the foreign key attribute onother
which refers to the primary key of its parent object. 由此可见,当与简单的一对多操作一起使用时,contains()
非常有用。对于多对多操作,
contains()
的行为有更多的注意事项。关联表将在语句中呈现,产生“隐式”连接,即在FROM子句中包含多个等同于WHERE子句的表:query(MyClass).filter(MyClass.contains(other))
产生如下查询:
SELECT * FROM my_table, my_association_table AS my_association_table_1 WHERE my_table.id = my_association_table_1.parent_id AND my_association_table_1.child_id = <some id>
Where
<some id>
would be the primary key ofother
. From the above, it is clear thatcontains()
will not work with many-to-many collections when used in queries that move beyond simple AND conjunctions, such as multiplecontains()
expressions joined by OR. 在这种情况下,需要使用子查询或明确的“外连接”。请参阅any()
查找使用EXISTS的较低性能替代方法,或参考Query.outerjoin()
以及Querying with Joins有关构建外连接的详细信息。
不同 T0> ( T1> ) T2> ¶ T3>
- inherited from the
distinct()
method ofColumnOperators
针对父对象生成一个
distinct()
子句。
endswith
( 其他,** kwargs ) t5 >- inherited from the
endswith()
method ofColumnOperators
实现'endswith'操作符。
在列上下文中,生成子句
LIKE '%&lt; other&gt;
有
( criterion = None,** kwargs ) / T5>使用EXISTS生成一个表达式,该表达式根据特定标准测试标量引用。
表达式如下:
session.query(MyClass).filter( MyClass.somereference.has(SomeRelated.x==2) )
将产生如下查询:
SELECT * FROM my_table WHERE EXISTS (SELECT 1 FROM related WHERE related.id==my_table.related_id AND related.x=2)
由于
has()
使用相关的子查询,因此与大型目标表相比,其性能几乎不如使用连接的性能好。has()
is only valid for scalar references, i.e. arelationship()
that hasuselist=False
. 对于集合引用,请使用any()
。
ilike
( 其他,escape =无 ) t5 >- inherited from the
ilike()
method ofColumnOperators
实现
ilike
运算符。在列上下文中,生成子句
a ILIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.ilike("%foobar%"))
参数:
在_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
产生一个IN子句 - 目前还没有为
relationship()
实现属性。
是_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
is_()
method ofColumnOperators
实现
IS
运算符。通常,当与
None
的值进行比较时,会自动生成IS
,这会解析为NULL
。但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS
。New in version 0.7.9.
is_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
is_distinct_from()
方法 tt>ColumnOperators
实现
IS DISTINCT FROM
运算符。在大多数平台上呈现“一个IS DISTINCT FROM b”;在一些如SQLite可能会呈现“一个不是b”。
版本1.1中的新功能
IsNot运算 T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
isnot()
method ofColumnOperators
实现
IS NOT
运算符。Normally,
IS NOT
is generated automatically when comparing to a value ofNone
, which resolves toNULL
. 但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS NOT
。New in version 0.7.9.
isnot_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现
IS NOT DISTINCT FROM
运算符。在大多数平台上呈现“不是从BIND DISTINCT FROM b”;在某些例如SQLite上可能会呈现“a IS b”。
版本1.1中的新功能
像
( 其他,escape =无 ) t5 >- inherited from the
like()
method ofColumnOperators
像运算符一样实现
like
在列上下文中,生成子句
a LIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.like("%foobar%"))
参数:
映射器 T0> ¶ T1>
由
RelationshipProperty.Comparator
引用的目标Mapper
。这是
relationship()
的“目标”或“远程”端。
匹配
( 其他,** kwargs ) t5 >- 继承自
match()
方法 tt>ColumnOperators
实现数据库特定的“匹配”运算符。
match()
attempts to resolve to a MATCH-like function or operator provided by the backend. 例子包括:- Postgresql - 呈现
x @@ to_tsquery(y)
- MySQL - renders
MATCH (x) AGAINST (y IN BOOLEAN MODE)
- Oracle - 呈现
CONTAINS(x, y)
- 其他后端可能会提供特殊的实现。
- 没有任何特殊实现的后端会将操作符发送为“MATCH”。例如,这与SQlite兼容。
- Postgresql - 呈现
-
notilike
(other, escape=None)¶ - inherited from the
notilike()
method ofColumnOperators
执行
NOT ILIKE
运算符。这相当于对
ColumnOperators.ilike()
使用否定,即~x.ilike(y)
。0.8版本中的新功能
notin _ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
notin_()
方法ColumnOperators
执行
NOT IN
运算符。这相当于对
ColumnOperators.in_()
,即~x.in_(y)
使用否定。0.8版本中的新功能
notlike
( 其他,转义=无 ) t5 >- inherited from the
notlike()
method ofColumnOperators
执行
NOT LIKE
运算符。这相当于对
ColumnOperators.like()
,即~x.like(y)
使用否定。0.8版本中的新功能
nullsfirst T0> ( T1> ) T2> ¶ T3>
- inherited from the
nullsfirst()
method ofColumnOperators
针对父对象生成
nullsfirst()
子句。
nullslast T0> ( T1> ) T2> ¶ T3>
针对父对象生成一个
nullslast()
子句。
of_type T0> ( T1> CLS T2> ) T3> ¶ T4>
生成一个表示父类的特定“子类型”属性的构造。
目前,这可与
Query.join()
和Query.outerjoin()
结合使用。
op
( opstring,precedence = 0,is_comparison = False ) T5> ¶ T6>产生通用的操作员功能。
例如。:
somecolumn.op("*")(5)
生产:
somecolumn * 5
该函数也可用于使按位运算符明确。例如:
somecolumn.op('&')(0xff)
是
somecolumn
中的值的按位与。参数: - operator¶ – a string which will be output as the infix operator between this element and the expression passed to the generated function.
- 优先顺序 ¶ -
当对表达式加括号时,优先级适用于运算符。较低的数字将使表达式在针对具有较高优先级的另一个运算符应用时加括号。除了逗号(
,
)和AS
运算符以外,0
的默认值低于所有运算符。100的值将会高于或等于所有操作员,-100将低于或等于所有操作员。New in version 0.8: - added the ‘precedence’ argument.
- is_comparison ¶ -
如果为True,那么该运算符将被视为“比较”运算符,即,其计算结果为boolean true / false值,如
==
,>
等。应该设置此标志,以便ORM关系可以确定运算符在自定义连接条件中使用时是比较运算符。版本0.9.2新增: - 添加了
Operators.op.is_comparison
标志。
操作 tt> ( op,*其他,** kwargs / T5> ¶ T6>
操作一个参数。
这是最低级别的操作,缺省情况下会引发
NotImplementedError
。在子类上覆盖它可以使普通行为适用于所有操作。例如,覆盖
ColumnOperators
将func.lower()
应用于左侧和右侧:class MyComparator(ColumnOperators): def operate(self, op, other): return op(func.lower(self), func.lower(other))
参数:
reverse_operate
( op,其他,** kwargs T5> ¶ T6>- inherited from the
reverse_operate()
method ofOperators
对参数进行反向操作。
用法与
operate()
相同。
startswith
( 其他,** kwargs ) ¶ t5 >- inherited from the
startswith()
method ofColumnOperators
实现
startwith
运算符。在列上下文中,生成子句
LIKE '&lt; other&gt;%'
-
RelationshipProperty.
__init__
(argument, secondary=None, primaryjoin=None, secondaryjoin=None, foreign_keys=None, uselist=None, order_by=False, backref=None, back_populates=None, post_update=False, cascade=False, extension=None, viewonly=False, lazy=True, collection_class=None, passive_deletes=False, passive_updates=True, remote_side=None, enable_typechecks=True, join_depth=None, comparator_factory=None, single_parent=False, innerjoin=False, distinct_target_key=None, doc=None, active_history=False, cascade_backrefs=True, load_on_pending=False, bake_queries=True, strategy_class=None, _local_remote_pairs=None, query_class=None, info=None)¶ 构建一个新的
RelationshipProperty
对象。这个构造函数被镜像为公共API函数;有关完整的用法和参数说明,请参阅
relationship()
。
RelationshipProperty。 T0>
级联 T1> ¶ T2>
返回该
RelationshipProperty
的当前级联设置。
RelationshipProperty。 T0>
class_attribute T1> ¶ T2>
- 继承自
MapperProperty
的class_attribute
属性返回与此
MapperProperty
对应的类绑定描述符。这基本上是一个
getattr()
调用:return getattr(self.parent.class_, self.key)
即如果这个
MapperProperty
被命名为addresses
,并且它映射到的类是User
,那么这个序列是可能的:>>> from sqlalchemy import inspect >>> mapper = inspect(User) >>> addresses_property = mapper.attrs.addresses >>> addresses_property.class_attribute is User.addresses True >>> User.addresses.property is addresses_property True
RelationshipProperty
extension_type
=符号('NOT_EXTENSION') ¶
RelationshipProperty。 T0>
初始化 T1> ( T2> ) T3> ¶ T4>
- inherited from the
init()
method ofMapperProperty
在创建所有映射器以调用映射器之间的关系并执行其他映射器创建初始化步骤后调用。
RelationshipProperty。 T0>
映射器 T1> ¶ T2>
返回这个
RelationshipProperty
的目标Mapper
。这是一个惰性初始化静态属性。
-
RelationshipProperty.
set_parent
(parent, init)¶ - inherited from the
set_parent()
method ofMapperProperty
设置引用此MapperProperty的父映射器。
当映射器第一次知道时,此方法被一些子类覆盖以执行额外的设置。
RelationshipProperty。 T0>
表 T1> ¶ T2>
返回链接到
RelationshipProperty
对象的目标Mapper
的可选项。从版本0.7开始弃用:使用.target
- class
- class
sqlalchemy.orm.descriptor_props.
SynonymProperty
(name, map_column=None, descriptor=None, comparator_factory=None, doc=None, info=None)¶ 基础:
sqlalchemy.orm.descriptor_props.DescriptorProperty
-
__init__
(name, map_column=None, descriptor=None, comparator_factory=None, doc=None, info=None)¶ 构建一个新的
SynonymProperty
对象。这个构造函数被镜像为公共API函数;有关完整的用法和参数说明,请参阅
synonym()
。
-
cascade_iterator
(type_, state, visited_instances=None, halt_on=None)¶ - inherited from the
cascade_iterator()
method ofMapperProperty
从MapperProperty开始,迭代与特定“级联”相关的实例。
返回一个iterator3-tuples(实例,映射器,状态)。
请注意,在调用cascade_iterator之前,此MapperProperty上的'cascade'集合将首先针对给定类型进行检查。
此方法通常仅适用于RelationshipProperty。
class_attribute T0> ¶ T1>
- 继承自
MapperProperty
的class_attribute
属性返回与此
MapperProperty
对应的类绑定描述符。这基本上是一个
getattr()
调用:return getattr(self.parent.class_, self.key)
即如果这个
MapperProperty
被命名为addresses
,并且它映射到的类是User
,那么这个序列是可能的:>>> from sqlalchemy import inspect >>> mapper = inspect(User) >>> addresses_property = mapper.attrs.addresses >>> addresses_property.class_attribute is User.addresses True >>> User.addresses.property is addresses_property True
-
create_row_processor
(context, path, mapper, result, adapter, populators)¶ - inherited from the
create_row_processor()
method ofMapperProperty
生成行处理函数并追加到给定的填充列表集。
do_init T0> ( T1> ) T2> ¶ T3>
- inherited from the
do_init()
method ofMapperProperty
执行子类特定的初始化后映射器创建步骤。
这是一个由
MapperProperty
对象的init()方法调用的模板方法。
extension_type
=符号('NOT_EXTENSION') ¶
初始化 T0> ( T1> ) T2> ¶ T3>
- inherited from the
init()
method ofMapperProperty
在创建所有映射器以调用映射器之间的关系并执行其他映射器创建初始化步骤后调用。
-
merge
(session, source_state, source_dict, dest_state, dest_dict, load, _recursive, _resolve_conflict_map)¶ - 继承自
MapperProperty
的merge()
合并由
MapperProperty
表示的属性从源到目标对象。
post_instrument_class T0> ( T1> 映射器 T2> ) T3> ¶ T4>
- inherited from the
post_instrument_class()
method ofMapperProperty
执行init()完成后需要进行的仪器调整。
给定的Mapper是调用操作的Mapper,在继承场景中可能不是与self.parent相同的映射器;但是,Mapper将始终至少成为self.parent的子映射器。
该方法通常由StrategizedProperty使用,该方法将其委派给LoaderStrategy.init_class_attribute()以对类绑定的InstrumentedAttribute执行最终设置。
-
setup
(context, entity, path, adapter, **kwargs)¶ - inherited from the
setup()
method ofMapperProperty
由Query调用以构造SQL语句。
与目标映射器关联的每个MapperProperty处理查询上下文引用的语句,并根据需要添加列和/或标准。
-
- class
sqlalchemy.orm.query。
QueryContext
( query / T5> ¶ T6>
- 类 T0>
sqlalchemy.orm.attributes。 T1>
QueryableAttribute T2> ( T3> 类_ T4>,键 T5>, IMPL =无 T6>,比较器=无 T7>, parententity =无 T8>, of_type =无 T9> ) T10> ¶ T11>
基础:
sqlalchemy.orm.base._MappedAttribute
,sqlalchemy.orm.base.InspectionAttr
,sqlalchemy.orm.interfaces.PropComparator
descriptor对象的基类,它代表
MapperProperty
对象拦截属性事件。实际的MapperProperty
可以通过QueryableAttribute.property
属性进行访问。__当量__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
实施
==
运算符。在列上下文中,生成子句
a = b
。If the target isNone
, producesa IS NULL
.
__文件__ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__le__()
method ofColumnOperators
实现
<=
运算符。在列上下文中,生成子句
a <= b
。
__ LT __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__lt__()
method ofColumnOperators
实现
<
运算符。在列上下文中,生成子句
a b
。
__ NE __ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
__ne__()
method ofColumnOperators
实现
!=
运算符。在列上下文中,生成子句
a != b
。If the target isNone
, producesa IS NOT NULL
.
适配器 T0> ¶ T1>
- 继承自
adapter
属性的PropComparator
生成一个可调用的列,适应列表达式以适应该比较器的别名版本。
任何
( criterion = None,** kwargs ) / T5>- inherited from the
any()
method ofPropComparator
如果此集合包含符合给定条件的任何成员,则返回true。
any()
的通常实现是RelationshipProperty.Comparator.any()
。参数:
任何_ T0> ( T1> ) T2> ¶ T3>
- inherited from the
any_()
method ofColumnOperators
针对父对象生成
any_()
子句。版本1.1中的新功能
ASC T0> ( T1> ) T2> ¶ T3>
- 继承自
asc()
ColumnOperators
针对父对象生成一个
asc()
子句。
( cleft,cright,symmetric = False T5> ¶ T6>
- inherited from the
between()
method ofColumnOperators
在()子句之间针对父对象生成
between()
整理 T0> ( T1> 整理 T2> ) T3> ¶ T4>
- inherited from the
collate()
method ofColumnOperators
根据给定的排序字符串,针对父对象生成一个
collate()
子句。
的concat T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现'concat'操作符。
在列上下文中,生成子句
a || b
,或者使用concat()
运算符在MySQL上。
包含
( 其他,** kwargs ) >- inherited from the
contains()
method ofColumnOperators
实现'包含'运算符。
在列上下文中,生成子句
LIKE '%&lt; other&gt;%'
不同 T0> ( T1> ) T2> ¶ T3>
- inherited from the
distinct()
method ofColumnOperators
针对父对象生成一个
distinct()
子句。
endswith
( 其他,** kwargs ) t5 >- inherited from the
endswith()
method ofColumnOperators
实现'endswith'操作符。
在列上下文中,生成子句
LIKE '%&lt; other&gt;
extension_type
=符号('NOT_EXTENSION') ¶
有
( criterion = None,** kwargs ) / T5>- inherited from the
has()
method ofPropComparator
如果此元素引用符合给定条件的成员,则返回true。
The usual implementation of
has()
isRelationshipProperty.Comparator.has()
.参数:
ilike
( 其他,escape =无 ) t5 >- inherited from the
ilike()
method ofColumnOperators
实现
ilike
运算符。在列上下文中,生成子句
a ILIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.ilike("%foobar%"))
参数:
在_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
in_()
method ofColumnOperators
在运算符中实现
in
在列上下文中,生成子句
a IN 其他
。“other”可以是列表达式的元组/列表,或者是select()
结构。
信息 T0> ¶ T1>
返回底层SQL元素的'info'字典。
这里的行为如下:
- If the attribute is a column-mapped property, i.e.
ColumnProperty
, which is mapped directly to a schema-levelColumn
object, this attribute will return theSchemaItem.info
dictionary associated with the core-levelColumn
object. - If the attribute is a
ColumnProperty
but is mapped to any other kind of SQL expression other than aColumn
, the attribute will refer to theMapperProperty.info
dictionary associated directly with theColumnProperty
, assuming the SQL expression itself does not have its own.info
attribute (which should be the case, unless a user-defined SQL construct has defined one). - If the attribute refers to any other kind of
MapperProperty
, includingRelationshipProperty
, the attribute will refer to theMapperProperty.info
dictionary associated with thatMapperProperty
. - To access the
MapperProperty.info
dictionary of theMapperProperty
unconditionally, including for aColumnProperty
that’s associated directly with aschema.Column
, the attribute can be referred to usingQueryableAttribute.property
attribute, asMyClass.someattribute.property.info
.
0.8.0版本中的新功能
- If the attribute is a column-mapped property, i.e.
是_ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
is_()
method ofColumnOperators
实现
IS
运算符。通常,当与
None
的值进行比较时,会自动生成IS
,这会解析为NULL
。但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS
。New in version 0.7.9.
is_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
is_distinct_from()
方法 tt>ColumnOperators
实现
IS DISTINCT FROM
运算符。在大多数平台上呈现“一个IS DISTINCT FROM b”;在一些如SQLite可能会呈现“一个不是b”。
版本1.1中的新功能
IsNot运算 T0> ( T1> 其他 T2> ) T3> ¶ T4>
- inherited from the
isnot()
method ofColumnOperators
实现
IS NOT
运算符。Normally,
IS NOT
is generated automatically when comparing to a value ofNone
, which resolves toNULL
. 但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS NOT
。New in version 0.7.9.
isnot_distinct_from T0> ( T1> 其他 T2> ) T3> ¶ T4>
实现
IS NOT DISTINCT FROM
运算符。在大多数平台上呈现“不是从BIND DISTINCT FROM b”;在某些例如SQLite上可能会呈现“a IS b”。
版本1.1中的新功能
像
( 其他,escape =无 ) t5 >- inherited from the
like()
method ofColumnOperators
像运算符一样实现
like
在列上下文中,生成子句
a LIKE 其他
。例如。:
select([sometable]).where(sometable.c.column.like("%foobar%"))
参数:
匹配
( 其他,** kwargs ) t5 >- 继承自
match()
方法 tt>ColumnOperators
实现数据库特定的“匹配”运算符。
match()
attempts to resolve to a MATCH-like function or operator provided by the backend. 例子包括:- Postgresql - 呈现
x @@ to_tsquery(y)
- MySQL - renders
MATCH (x) AGAINST (y IN BOOLEAN MODE)
- Oracle - 呈现
CONTAINS(x, y)
- 其他后端可能会提供特殊的实现。
- 没有任何特殊实现的后端会将操作符发送为“MATCH”。例如,这与SQlite兼容。
- Postgresql - 呈现
-
notilike
(other, escape=None)¶ - inherited from the
notilike()
method ofColumnOperators
执行
NOT ILIKE
运算符。这相当于对
ColumnOperators.ilike()
使用否定,即~x.ilike(y)
。0.8版本中的新功能
notin _ T0> ( T1> 其他 T2> ) T3> ¶ T4>
- 继承自
notin_()
方法ColumnOperators
执行
NOT IN
运算符。这相当于对
ColumnOperators.in_()
,即~x.in_(y)
使用否定。0.8版本中的新功能
notlike
( 其他,转义=无 ) t5 >- inherited from the
notlike()
method ofColumnOperators
执行
NOT LIKE
运算符。这相当于对
ColumnOperators.like()
,即~x.like(y)
使用否定。0.8版本中的新功能
nullsfirst T0> ( T1> ) T2> ¶ T3>
- inherited from the
nullsfirst()
method ofColumnOperators
针对父对象生成
nullsfirst()
子句。
nullslast T0> ( T1> ) T2> ¶ T3>
针对父对象生成一个
nullslast()
子句。
op
( opstring,precedence = 0,is_comparison = False ) T5> ¶ T6>产生通用的操作员功能。
例如。:
somecolumn.op("*")(5)
生产:
somecolumn * 5
该函数也可用于使按位运算符明确。例如:
somecolumn.op('&')(0xff)
是
somecolumn
中的值的按位与。参数: - operator¶ – a string which will be output as the infix operator between this element and the expression passed to the generated function.
- 优先顺序 ¶ -
当对表达式加括号时,优先级适用于运算符。较低的数字将使表达式在针对具有较高优先级的另一个运算符应用时加括号。除了逗号(
,
)和AS
运算符以外,0
的默认值低于所有运算符。100的值将会高于或等于所有操作员,-100将低于或等于所有操作员。New in version 0.8: - added the ‘precedence’ argument.
- is_comparison ¶ -
如果为True,那么该运算符将被视为“比较”运算符,即,其计算结果为boolean true / false值,如
==
,>
等。应该设置此标志,以便ORM关系可以确定运算符在自定义连接条件中使用时是比较运算符。版本0.9.2新增: - 添加了
Operators.op.is_comparison
标志。
父 T0> ¶ T1>
返回代表父级的检查实例。
这将是
Mapper
或AliasedInsp
的实例,具体取决于与此属性关联的父实体的性质。
属性 T0> ¶ T1>
返回与此
QueryableAttribute
关联的MapperProperty
。这里的返回值通常是
ColumnProperty
或RelationshipProperty
的实例。
startswith
( 其他,** kwargs ) ¶ t5 >- inherited from the
startswith()
method ofColumnOperators
实现
startwith
运算符。在列上下文中,生成子句
LIKE '&lt; other&gt;%'
- class
sqlalchemy.orm.session.
UOWTransaction
(session)¶ -
filter_states_for_dep
(dep, states)¶ 将InstanceState的给定列表筛选为与给定的DependencyProcessor相关的列表。
finalize_flush_changes T0> ( T1> ) T2> ¶ T3>
在成功刷新()后将处理的对象标记为清除/删除。
在execute()方法成功且事务已提交后,在flush()方法内调用此方法。
-
get_attribute_history
(state, key, passive=symbol('PASSIVE_NO_INITIALIZE'))¶ 外观到attributes.get_state_history(),包括缓存结果。
IS_DELETED T0> ( T1> 状态 T2> ) T3> ¶ T4>
如果在此uowtransaction中将给定状态标记为已删除,则返回true。
remove_state_actions T0> ( T1> 状态 T2> ) T3> ¶ T4>
从uowtransaction中移除一个状态的待处理动作。
was_already_deleted T0> ( T1> 状态 T2> ) T3> ¶ T4>
如果给定的状态已过期并且之前被删除,则返回true。
-