help wantedtype:question
Description
Description
the same question: https://github.com/go-gorm/gorm/issues/5754 https://github.com/go-gorm/gorm/issues/3810
My experimental environment is as follows: gorm version 1.24.5 mysql 5.7 go 1.19.1
At the same time, I wrote a simple CreateInBatches OnConflict function. When writing data, if the email has repeated updates to the UpdatedAt field.
type User struct {
ID uint64
Email string `gorm:"uniqueIndex:idx_email;type:varchar(256);"`
UpdatedAt time.Time
CreatedAt time.Time
}
func (u *user) Create(ctx context.Context, models []*model.User) error {
return u.db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "email"}},
DoUpdates: clause.AssignmentColumns([]string{"updated_at"}),
}).CreateInBatches(models, 100).Error
}
Assume that the current user table has the following data
| ID | Emaul | UpdatedAt | CreatedAt |
|---|---|---|---|
| 1 | test@test.com | 2023-02-11 17:50:51.243 | 2023-02-11 17:50:51.243 |
| 2 | apple@apple.com | 2023-02-11 17:50:51.243 | 2023-02-11 17:50:51.243 |
Now I need to insert two data, namely test@test.com and apple@apple.com. I want to update the UpdatedAt field.
gorm does the following.
INSERT INTO `users` (`email`, `created_at`, `updated_at`)
VALUES ('est@test.com', '2023-02-12 01:55:44.966', '2023-02-12 01:55:44.966'),
('test-mail2', '2023-02-12 01:55:44.966', '2023-02-12 01:55:44.966')
ON DUPLICATE KEY UPDATE `updated_at`=VALUES(`updated_at`)
The user id I got was wrong, and the id started from 2, which is incredible.
Sincerely hope you can help me solve this problem, thank you. @jinzhu