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

Goroutine insert self-incrementing id duplication #1757

Open
whrss9527 opened this issue May 6, 2023 · 2 comments
Open

Goroutine insert self-incrementing id duplication #1757

whrss9527 opened this issue May 6, 2023 · 2 comments

Comments

@whrss9527
Copy link

Goroutine insert self-incrementing id duplication

@fulghum
Copy link
Contributor

fulghum commented May 8, 2023

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?

@whrss9527 whrss9527 reopened this Jun 19, 2023
@whrss9527
Copy link
Author

whrss9527 commented Jun 19, 2023

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.

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

No branches or pull requests

3 participants