forked from mirror/gorm
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:
parent
26fde9110f
commit
588b598f9f
|
@ -76,7 +76,9 @@ func updateCallback(scope *Scope) {
|
|||
for _, field := range scope.Fields() {
|
||||
if scope.changeableField(field) {
|
||||
if !field.IsPrimaryKey && field.IsNormal {
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue