dolthub / go-mysql-server

A MySQL-compatible relational database with a storage agnostic query engine. Implemented in pure Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Goroutine insert self-incrementing id duplication

whrss9527 opened this issue · comments

Goroutine insert self-incrementing id duplication

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?

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.