1、先看下文档

merge(instanceload=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:
  • instance – Instance to be merged.
  • load –

    Boolean, when False, merge() 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 Session from a second level cache, or to transfer just-loaded objects into the Session 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 Session. 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

sqlalchemy的merge使用的更多相关文章

  1. sqlalchemy学习

    sqlalchemy官网API参考 原文作为一个Pythoner,不会SQLAlchemy都不好意思跟同行打招呼! #作者:笑虎 #链接:https://zhuanlan.zhihu.com/p/23 ...

  2. python SQLAlchemy

    这里我们记录几个python SQLAlchemy的使用例子: 如何对一个字段进行自增操作 user = session.query(User).with_lockmode('update').get ...

  3. sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO

    sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...

  4. SQLAlchemy模型使用

    SQLAchemy模型使用 简介: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用 ...

  5. Tornado sqlalchemy

    上篇文章提到了,最近在用 Python 做一个网站.除了 Tornado ,主要还用到了 SQLAlchemy.这篇就是介绍我在使用 SQLAlchemy 的过程中,学到的一些知识. 首先说下,由于最 ...

  6. sqlalchemy - day2

     Relationship Configuration 一.one to many 直接上代码 from sqlalchemy import create_engine engine = create ...

  7. sqlalchemy - day1

    一.Create engine Database url规则: dialect+driver://username:password@host:port/database echo: True表示cm ...

  8. SQLAlchemy使用介绍

    SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers ...

  9. Sqlalchemy python经典第三方orm

    Ⅰ. 安装 pip install sqlalchemy Ⅱ. 起步链接 import time import threading import sqlalchemy from sqlalchemy ...

随机推荐

  1. 【树形背包】bzoj4033: [HAOI2015]树上染色

    仔细思考后会发现和51nod1677 treecnt有异曲同工之妙 Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 ...

  2. lnmp一键安装包 虚拟主机问题

    lnmp一键安装包淌过的坑  --手动虚拟主机配置 安装一键包的时候教程 官网也有虚拟主机的教程 一下示例: 后来自己手动去做 就遇到了一个大家都遇到的问题 及时安装让nginx支持解析PHP脚本解析 ...

  3. CSS3-文本-word-wrap,word-break,white-space

    一.word-wrap使用: 语法: word-wrap : normal | break-word 取值说明: 1.normal为默认值,当其值为normal控制连续文本换行(允许内容顶开容器的边界 ...

  4. 查找闲置ip

    首先ping 一下该网段的广播地址,如ping 192.168.0.255 ,然后使用arp -a就可以查看到那些地址是被使用的,以及网卡的mac值.剩余的是暂时还未被使用的,当然没开机的机器就看不出 ...

  5. include/autoconfig.mk

    把autoconfig.mk和/include/configs/ $(obj)include/autoconf.mk.dep: $(obj)include/config.h include/commo ...

  6. TSOJ--方格上的路径

    题目描述: 试求 n × m 的方格图形中,从点 (0, 0) 到点 (n, m) 的最短路径数目. 输入描述: 有多组测试数据.输入的第一行为一个正整数 N,表示接下来有 N 组测试数据. 在接下来 ...

  7. struts 乱码

    在进行struts开发的过程中,总也是出现很多的乱码问题 ,但归根到底,也只是以下三种情况: ㈠页面显示中文乱码 ㈡传递参数中文乱码 ㈢国际化资源文件乱码 下面就这三中情况介绍怎么在具体项目中处理这些 ...

  8. 00049_super关键字

    1.子父类中构造方法的调用 (1)在创建子类对象时,父类的构造方法会先执行,因为子类中所有构造方法的第一行有默认的隐式super();语句: (2)格式 调用本类中的构造方法 this(实参列表); ...

  9. 大数据学习——sqoop安装

    1上传  sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz 2解压 .bin__hadoop--alpha.tar.gz 3重命名 .bin__hadoop--al ...

  10. Dell Idrac Normal Settings

    racadm安装请查看:http://www.cnblogs.com/zyd112/p/7611022.html racadm语法(远程执行命令):racadm -r <racIpAddr> ...