sqlalchemy的merge使用
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