检查模块提供了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对象。
| 参数: |
|
|---|
以下列出了许多最常见的检查目标。
Connectable(即Engine,Connection) - 返回一个Inspector对象。ClauseElement - all SQL expression components, including Table, Column, serve as their own inspection objects, meaning any of these objects passed to inspect() return themselves.object - 给定的对象将由ORM检查映射 - 如果是,则返回表示对象映射状态的InstanceState。InstanceState还通过AttributeState接口提供对每个属性状态的访问,以及通过History访问任何属性的每次刷新“历史记录”目的。type(即类) - 给定的类将由ORM检查映射 - 如果是,则返回该类的Mapper。inspect(), such as inspect(MyClass.some_attribute), returns a QueryableAttribute object, which is the descriptor associated with a mapped class. This descriptor refers to a MapperProperty, which is usually an instance of ColumnProperty or RelationshipProperty, via its QueryableAttribute.property attribute.AliasedClass - 返回一个AliasedInsp对象。