db.CreateInBatches undefined (type *gorm.DB has no field or method CreateInBatches)

db.CreateInBatches undefined (type *gorm.DB has no field or method CreateInBatches)

在这里插入图片描述
根据官方的代码测试批量插入数据出现以下问题

package main

import (
	"fmt"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
)

type Student struct {
	gorm.Model
	Username string `gorm:"type:varchar(20);not null " `
	Password string `gorm:"type:varchar(500);not null" `
	Age      int    `gorm:"type:int;DEFAULT:18" `
}

func main() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	db, err := gorm.Open("mysql",
		fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
			"root", "123456", "127.0.0.1", "3306", "ginblog",
		))
	if err != nil {
		fmt.Println("连接数据库出错:", err)
	}
	db.SingularTable(true)
	db.AutoMigrate(&Student{})
	// user := Student{Username: "Jinzhu", Age: 18, Password: "123456"}

	// result := db.Create(&user) // 通过数据的指针来创建
	// fmt.Println(result)

	var students = []Student{{Username: "jinzhu5", Password: "123456"}, {Username: "jinzhu2", Password: "123456"}, {Username: "jinzhu3", Password: "123456"}}
	db.CreateInBatches(&students, 1)

	// for _, student := range students {
	// 	student.ID //
	// }
}

官方第一种方法

.\demo01.go:34:11: too many arguments in call to db.Create
have (*[]Student, number)
want (interface {})

报错

官方第二种方法

db.CreateInBatches undefined (type *gorm.DB has no field or method CreateInBatches)

解决方案

还在研究中~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值