Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在批量插入时,使用了clause.OnConflict{DoNothing: true},回填的ID是有问题的,在注释中并未提到这一点 #7393

Open
ydzh opened this issue Mar 15, 2025 · 2 comments
Labels

Comments

@ydzh
Copy link

ydzh commented Mar 15, 2025

https://github.com/go-gorm/gorm/blob/a9d27293de2267a36fa6c9f8892977d3159cf8ea/callbacks/create.go#L134C3-L134C123
如下代码可验证:

type TestTable struct {
	ID   int    `gorm:"primaryKey;autoIncrement"`
	Num  uint   `gorm:"uniqueIndex:num_index"`
	Name string `gorm:"size:45"`
}

func main() {
	// 数据库连接信息
	dsn := "root:jt20032217@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("无法连接到数据库: " + err.Error())
	}

	fmt.Println("✅ 成功连接到 MySQL")

	// 插入数据
	datas := []TestTable{
		{Num: 50, Name: "张三"},
		{Num: 51, Name: "李四"},
		{Num: 52, Name: "王五"},
	}
	if err := db.WithContext(context.Background()).Table("test_table").Clauses(clause.OnConflict{DoNothing: true}).Create(&datas).Error; err != nil {
		panic("插入数据失败: " + err.Error())
	}
	for _, data := range datas {
		fmt.Printf("插入数据成功: %+v\n", data)
	}
}

数据库原始数据:
+-----+------+-------+
| id | num | name |
+-----+------+-------+
| 138 | 28 | Alice |
| 139 | 35 | Bob |
| 140 | 30 | Ajen |
| 141 | 20 | Tom |
| 142 | 51 | Mary |
+-----+------+-------+
执行完数据:
mysql> select * from test_table;
+-----+------+--------+
| id | num | name |
+-----+------+--------+
| 138 | 28 | Alice |
| 139 | 35 | Bob |
| 140 | 30 | Ajen |
| 141 | 20 | Tom |
| 142 | 51 | Mary |
| 169 | 50 | 张三 |
| 170 | 52 | 王五 |
+-----+------+--------+
mysql innodb_autoinc_lock_mode
mysql> SHOW VARIABLES LIKE 'innodb_autoinc_lock_mode';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_autoinc_lock_mode | 1 |
+--------------------------+-------+
符合注释中的要求
程序输出结果:

Image

ID 170、171于db实际数据不一致

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Mar 15, 2025
@ydzh
Copy link
Author

ydzh commented Mar 15, 2025

在innodb_autoinc_lock_mode=1时,simple insert的id都是预先分配的,唯一键冲突时并不会真的执行插入,使用LastInsertId倒推并不能还原ID

Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant