I am having an issue with my structs not being able to update values for foreign keys with GORM. When I use the Save method to update the AccountStatusID the value is not updated but it does work when I use the Updates method. At the same time the Save method works when I want to update the Name field.
I am using GORM version x.x.x.
Structs:
type User struct {
gorm.Model
Name string
AccountStatusID uint
AccountStatus AccountStatus `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}
type AccountStatus struct {
gorm.Model
Name string `gorm:"unique"`
}
Example code for the failing update:
userM.AccountStatusID = 2
result := r.DB.Save(&userM)
Example code for the successful update:
result := r.DB.Model(&userM).Updates(models.User{AccountStatusID: 2})
At the same time this works
userM.Name = "John"
result := r.DB.Save(&userM)
But this doesn't
result := r.DB.Model(&userM).Updates(models.User{Name: "John"})
## Updated Queries Related Docs
Am I missing something? Or Is this the expected behaviour?
Comment From: saeidee
I can reproduce the issue, please create a playground PR here: https://github.com/go-gorm/playground