Skip to content

Commit

Permalink
Fix issue updating models with foreign key constraints (go-gorm#1988)
Browse files Browse the repository at this point in the history
* fix update callback to not try to write zero values when field has default value

* fix to update callback for gorm tests
  • Loading branch information
fillup authored and jinzhu committed Sep 9, 2018
1 parent 26fde91 commit 588b598
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func updateCallback(scope *Scope) {
for _, field := range scope.Fields() {
if scope.changeableField(field) {
if !field.IsPrimaryKey && field.IsNormal {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
}
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
for _, foreignKey := range relationship.ForeignDBNames {
if foreignField, ok := scope.FieldByName(foreignKey); ok && !scope.changeableField(foreignField) {
Expand Down

0 comments on commit 588b598

Please sign in to comment.