在仔细查看了SQLAlchemy的源代码之后,它看起来像是
add()插入时的记录:https
:
//github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/session.py#L1719
相关代码段:
def _save_impl(self, state): if state.key is not None: raise sa_exc.InvalidRequestError( "Object '%s' already has an identity - it can't be registered " "as pending" % state_str(state)) self._before_attach(state) if state not in self._new: self._new[state] = state.obj() state.insert_order = len(self._new) # THE INSERT ORDER IS SAVED! self._attach(state)
这是从
Session.add=>
self._save_or_update_state=>
self._save_or_update_impl=>调用的
self._save_impl。
然后在
_sort_states保存时使用它:https
:
//github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/persistence.py#L859
不幸的是,这仅仅是实现级别的证明。我在文档中找不到任何可以保证的内容…
更新 :从那以后,我对它进行了更多研究,结果发现有一个名为SQLAlchemy 的工作单元
概念,它在刷新时有点定义了顺序:http :
//www.aosabook.org/en/sqlalchemy.html(搜索工作单元)。
在同一类中,顺序实际上由
add调用的顺序确定。但是,您可能会在不同类之间的INSERT中看到不同的顺序。如果您添加
a类型为object的对象,
A然后又添加
b类型为object的对象
B,但是却
a发现有外键
b,则在INSERT
for
b之前,您将看到INSERT
a。



