Goroutine insert self-incrementing id duplication
whrss9527 opened this issue · comments
了迹奇有没 commented
Goroutine insert self-incrementing id duplication
Jason Fulghum commented
Hey @whrsss, thanks for opening an issue with us. Can you give us some more description of what you're seeing, what you expected, and some repro code so we can try to help?
了迹奇有没 commented
ERROR LOG:
2023/06/19 18:08:42 /xxx/asset_record.go:11 Error 1062: duplicate primary key given: [4]
[0.638ms] [rows:0] INSERT INTO `asset_record` (`user_id`,`asset_id`,`different`,`type`,`action`,`time`) VALUES ('user1','1',2000,1,'LoverUsecase.LevelUpReward','2023-06-19 18:08:42.752')
ERROR msg=Error 1062: duplicate primary key given: [4]
Model:
type AssetRecord struct {
ID uint `gorm:"primarykey,autoIncrement"`
UserId string `gorm:"column:user_id"`
AssetId string `gorm:"column:asset_id"`
Different int64 `gorm:"column:different"`
Type int32 `gorm:"column:type"`
Action string `gorm:"column:action"`
Time time.Time `gorm:"column:time"`
}
Logic:
func xxx(){
go func() {
record := model.AssetRecord{
UserId: userId,
AssetId: asset.Id,
Different: int64(asset.Count),
Type: asset.Type,
Action: action,
Time: time.Now(),
}
err := mysql.CreateAssetRecord(db.DB, record)
if err != nil {
log.Error(err)
}
}()
}
It doesn't always go wrong, I ran the unit test repeatedly today and occasionally.