Your Question
I ran into an issue where I was unable to update users records with a model similar to this (basically, the user record wouldnt apply the new data to the column):
type User struct {
Firstname string `gorm:"not null;column:firstname" json:"firstname"`
Lastname string `gorm:"default:null;column:lastname" json:"lastname"`
Username string `gorm:"default:null;column:username" json:"username"`
Email string `gorm:"not null;column:email" json:"email"`
}
The updates were done as follows:
for i := range users {
db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&users[i])
}
Upon changing to:
type User struct {
Firstname string `gorm:"not null;column:firstname" json:"firstname"`
Lastname string `gorm:"not null;column:lastname" json:"lastname"`
Username string `gorm:"not null;column:username" json:"username"`
Email string `gorm:"not null;column:email" json:"email"`
}
Changing default:null to not null for affected columns corrected the issue. It's as if the default:null columns were not actually being updated with OnConflict. The initial issue was that we noticed that username changes were not being updated or upserted properly. Is this by design or a bug?
GORM version that I'm using is gorm.io/gorm v1.21.10 with mysql
The document you expected this should be explained
https://gorm.io/docs/create.html#Upsert-On-Conflict
Expected answer
Just like it says in the above link:
Update all columns, except primary keys, to new value on conflict
The columns that were default:null were not updated unless they were changed to not null.
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 2 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 ✨
Comment From: pjarmalavicius
@jinzhu Why is this issue closed? It's a real and important bug
Comment From: mohuishou
+1