sqlalchemy - How does Session tracks changes in sqlalchemy?
问题描述
I studied for a while the sqlalchemy library. Besides its usefulness, I find this library very educational, as it covers pythonic ways of implementing things (like the instrumentation via descriptors) and its overall architecture.
Yet, there are some principles that I would like clarification about.
The concept of InstanceState
and its connection to the Session
object:
I looked in an article that explains the architecture of the library, for understanding the idea of InstanceState
and tracking changes of objects, but I would still like an explanation:
Each instance of instrumented class has an InstanceState
object in it (the _sa_instance_state
attribute) as in the chart from the article above:
Session overview chart
Yet, how does it really track the changes of the object?
When the Session
flushes the changes to the db, does it actually implement the changes that are tracked and found inside the InstanceState
? (Is it like the Session
perform iteration over all of the objects in his identity map and checks which one has changes in his InstanceState
, or when chaning objects the InstanceState
itself of an object notifies the Session
that some changes occured to this object?)
And when does the Mapper
actually enter the picture? It saves the correlation between the class attributes and the table columns, so when a change is taking place on an object, does the InstanceState
is using the mapper
to log what changes are really needed to be performed in the db?
Or is it other part of the ORM system that actually uses it? as this answer simulates the Mapper
to obesever in the subject-observer pattern:
Think of instrumentation as transparently adding a "Subject" implementation to a mapped class for the "Observer" mapper in a subject-observer pattern.
解决方案
推荐阅读
- .htaccess - htaccess force https 阻止下游代理规则
- pandas - 过滤Dataframe时出错(TypeError:无效类型比较)
- symfony - 在 Symfony 4 上调用 null 上的成员函数 encodePassword()
- java - 基于jsp的java应用程序中Aspectj的使用
- automapper - AutoMapper 8 升级 ConstructUsing
- sql - SQL Server:验证两列是否处于相同的排序顺序
- r - purrr::pmap 与其他默认输入
- itext7 - 在 iText 7 中标记文本后缺少字体资源字典
- node.js - 容器启动时如何运行命令 - Docker
- mysql - 被指定两次,既作为“更新”的目标,又作为数据的单独源