1、先看下文档
merge(instance, load=True)
Copy the state of a given instance into a corresponding instance within this Session.
Session.merge() examines the primary key attributes of the source instance, and attempts to reconcile it with an instance of the same primary key in the session. If not found locally, it attempts to load the object from the database based on primary key, and if none can be located, creates a new instance. The state of each attribute on the source instance is then copied to the target instance. The resulting target instance is then returned by the method; the original source instance is left unmodified, and un-associated with the Session if not already.
This operation cascades to associated instances if the association is mapped with cascade="merge".
See Merging for a detailed discussion of merging.
Changed in version 1.1: - Session.merge() will now reconcile pending objects with overlapping primary keys in the same way as persistent. See Session.merge resolves pending conflicts the same as persistent for discussion.
| Parameters: |
|
|---|
2、简单说下,merge的作用是合并,查找primary key是否一致,一致则合并,不一致则新建
参考:
1、http://docs.sqlalchemy.org/en/latest/orm/session_api.html
本文介绍了SQLAlchemy的merge方法,该方法通过检查源实例的主键,尝试与会话中相同主键的实例合并。如果在本地找不到,它会尝试根据主键从数据库加载对象。如果无法定位,则创建新实例。merge操作适用于关联映射为cascade的情况,并在1.1版本中改进了处理冲突的方式。
828

被折叠的 条评论
为什么被折叠?



