版本:1.1.0b2 |发布日期:2016年7月1日
SQLAlchemy 1.1文档
运行时检测API ¶
检查模块提供了inspect()
函数,该函数在Core和ORM中传递有关各种SQLAlchemy对象的运行时信息。
inspect()
函数是SQLAlchemy的公共API的入口点,用于查看内存中对象的配置和构造。根据传递给inspect()
的对象的类型,返回值可以是提供已知接口的相关对象,或者在许多情况下它将返回对象本身。
inspect()
的基本原理是双重的。One is that it replaces the need to be aware of a large variety of “information getting” functions in SQLAlchemy, such as Inspector.from_engine()
, orm.attributes.instance_state()
, orm.class_mapper()
, and others. 另一个原因是,inspect()
的返回值保证服从一个记录的API,因此允许构建在SQLAlchemy配置之上的第三方工具以前向兼容的方式构建。
0.8版中的新功能从版本0.8开始引入inspect()
系统。
-
sqlalchemy.inspection.
inspect
(subject, raiseerr=True)¶ 为给定的目标生成检查对象。
在某些情况下,返回的值可能与给定的对象相同,例如传递
Mapper
对象。在其他情况下,它将是给定对象的注册检查类型的实例,例如,如果传递engine.Engine
,则返回一个Inspector
对象。参数: - subject¶ – the subject to be inspected.
- raiseerr¶ – When
True
, if the given subject does not correspond to a known SQLAlchemy inspected type,sqlalchemy.exc.NoInspectionAvailable
is raised. 如果False
,则返回None
。
可用的检查目标¶
以下列出了许多最常见的检查目标。
Connectable
(即Engine
,Connection
) - 返回一个Inspector
对象。ClauseElement
- all SQL expression components, includingTable
,Column
, serve as their own inspection objects, meaning any of these objects passed toinspect()
return themselves.object
- 给定的对象将由ORM检查映射 - 如果是,则返回表示对象映射状态的InstanceState
。InstanceState
还通过AttributeState
接口提供对每个属性状态的访问,以及通过History
访问任何属性的每次刷新“历史记录”目的。type
(即类) - 给定的类将由ORM检查映射 - 如果是,则返回该类的Mapper
。- mapped attribute - passing a mapped attribute to
inspect()
, such asinspect(MyClass.some_attribute)
, returns aQueryableAttribute
object, which is the descriptor associated with a mapped class. This descriptor refers to aMapperProperty
, which is usually an instance ofColumnProperty
orRelationshipProperty
, via itsQueryableAttribute.property
attribute. AliasedClass
- 返回一个AliasedInsp
对象。