Advertisement

Gorm批次插入问题

阅读量:

问题描述

在项目初始化阶段,通过Gorm操作MySQL数据库,并将若干条原始数据录入到指定的表t1中。

此外,在每次执行时都会被执行;因此,在初始化操作中,Insert应为Upsert操作,也可能是FirstCREATE*操作

问题1:Gorm中对**Upsert**的支持并不友好

  • 通过使用Mysql的on duplicate key upset 语法来实现为Upsert
    • mysql数据库下, 仅根据表上的唯一索引(包括主键)来判断记录是否重复; 为了在MySQL中执行 Upsert 操作必须确保数据的一致性需先创建唯一约束, 并在字段定义时指定 gorm:"uniqueIndex:xxxx"

问题2:Gorm中**FirstOrCreate** 不支持批量插入;Create 支持批量插入

传入的参数是的不定长切片,调用**FirstOrCreate** 会失败

Function Description: Find the first matching record and optionally create a new one based on specified conditions when the conditions are of struct or map typ

全部评论 (0)

还没有任何评论哟~