Fix issue updating models with foreign key constraints (#1988)

* fix update callback to not try to write zero values when field has default value

* fix to update callback for gorm tests
This commit is contained in:
Phillip Shipley 2018-09-09 18:50:22 -04:00 committed by Jinzhu
parent 26fde9110f
commit 588b598f9f
1 changed files with 3 additions and 1 deletions

View File

@ -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) {