博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sqlalchemy的merge使用
阅读量:6001 次
发布时间:2019-06-20

本文共 2191 字,大约阅读时间需要 7 分钟。

1、先看下文档

merge(instanceload=True)

Copy the state of a given instance into a corresponding instance within this .

 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  if not already.

This operation cascades to associated instances if the association is mapped with cascade="merge".

See  for a detailed discussion of merging.

Changed in version 1.1: -  will now reconcile pending objects with overlapping primary keys in the same way as persistent. See  for discussion.

Parameters:
  • instance – Instance to be merged.
  • load –

    Boolean, when False,  switches into a “high performance” mode which causes it to forego emitting history events as well as all database access. This flag is used for cases such as transferring graphs of objects into a  from a second level cache, or to transfer just-loaded objects into the  owned by a worker thread or process without re-querying the database.

    The load=False use case adds the caveat that the given object has to be in a “clean” state, that is, has no pending changes to be flushed - even if the incoming object is detached from any . This is so that when the merge operation populates local attributes and cascades to related objects and collections, the values can be “stamped” onto the target object as is, without generating any history or attribute events, and without the need to reconcile the incoming data with any existing related objects or collections that might not be loaded. The resulting objects from load=False are always produced as “clean”, so it is only appropriate that the given objects should be “clean” as well, else this suggests a mis-use of the method.

2、简单说下,merge的作用是合并,查找primary key是否一致,一致则合并,不一致则新建

参考:

1、http://docs.sqlalchemy.org/en/latest/orm/session_api.html

转载地址:http://hebmx.baihongyu.com/

你可能感兴趣的文章
matplotlib绑定到PyQt5(有菜单)
查看>>
利用Powershell和ceye.io实现Windows账户密码回传
查看>>
如何清理EBS R12 middle-tier cache
查看>>
Windows 8.1 今年 1 月市场份额超 Vista
查看>>
《设计团队协作权威指南》—第1章1.5节总结
查看>>
【PMP认证考试之个人总结】第 5 章 项目时间管理
查看>>
Chair:支付宝前端团队推出的Node.js Web框架
查看>>
port-forward v1.0.1 发布,端口转发工具
查看>>
《Total Commander:万能文件管理器》——第3.8节.后续更新
查看>>
BSD vi/vim 命令大全(下)[转]
查看>>
css3中变形与动画(一)
查看>>
[XMove-自主设计的体感解决方案] 系统综述
查看>>
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
查看>>
【LINUX学习】磁盘分割之建立primary和logical 分区
查看>>
【YUM】第三方yum源rpmforge
查看>>
IOS(CGGeometry)几何类方法总结
查看>>
学会放下包袱,热爱单例
查看>>
一个通用并发对象池的实现
查看>>
才知道系列之GroupOn
查看>>
⑲云上场景:超级减肥王,基于OSS的高效存储实践
查看>>