GORM Playground Link
https://github.com/go-gorm/playground/pull/705
Description
betch save Conflict by uniqueIndex, id err
CREATE TABLE `users` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_users_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` (`id`, `name`)
VALUES
(1,'user1'),
(2,'user2');
type User struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `gorm:"uniqueIndex;size:100" json:"name"`
}
user1 := User{
Name: "user2",
}
user2 := User{
Name: "user3",
}
users := []User{user1, user2}
db.Debug().Save(users)
fmt.Println(users[0].ID, users[1].ID)
output
3 4
except
2 3
mysql data
id name
---
1 user1
2 user2
3 user3
Comment From: github-actions[bot]
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.io ✨ Search Before Asking ✨