Continue to update tracking fields even not selected with Select, but skip them if omited with Omit, fix #3856

This commit is contained in:
Jinzhu 2020-12-15 11:18:29 +08:00
parent 14a0976dd4
commit 0f00493c50
3 changed files with 16 additions and 16 deletions

View File

@ -244,7 +244,7 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
for _, db := range stmt.Schema.DBNames { for _, db := range stmt.Schema.DBNames {
if field := stmt.Schema.FieldsByDBName[db]; !field.HasDefaultValue || field.DefaultValueInterface != nil { if field := stmt.Schema.FieldsByDBName[db]; !field.HasDefaultValue || field.DefaultValueInterface != nil {
if v, ok := selectColumns[db]; (ok && v) || (!ok && !restricted) { if v, ok := selectColumns[db]; (ok && v) || (!ok && (!restricted || field.AutoCreateTime > 0 || field.AutoUpdateTime > 0)) {
values.Columns = append(values.Columns, clause.Column{Name: db}) values.Columns = append(values.Columns, clause.Column{Name: db})
} }
} }

View File

@ -202,7 +202,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
for _, dbName := range stmt.Schema.DBNames { for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.LookUpField(dbName) field := stmt.Schema.LookUpField(dbName)
if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil { if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil {
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { if v, ok := selectColumns[field.DBName]; (ok && v) || !ok {
now := stmt.DB.NowFunc() now := stmt.DB.NowFunc()
assignValue(field, now) assignValue(field, now)
@ -226,10 +226,9 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
for _, dbName := range stmt.Schema.DBNames { for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.LookUpField(dbName) field := stmt.Schema.LookUpField(dbName)
if !field.PrimaryKey || (!updatingValue.CanAddr() || stmt.Dest != stmt.Model) { if !field.PrimaryKey || (!updatingValue.CanAddr() || stmt.Dest != stmt.Model) {
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && (!restricted || (!stmt.SkipHooks && field.AutoUpdateTime > 0))) {
value, isZero := field.ValueOf(updatingValue) value, isZero := field.ValueOf(updatingValue)
if !stmt.SkipHooks { if !stmt.SkipHooks && field.AutoUpdateTime > 0 {
if field.AutoUpdateTime > 0 {
if field.AutoUpdateTime == schema.UnixNanosecond { if field.AutoUpdateTime == schema.UnixNanosecond {
value = stmt.DB.NowFunc().UnixNano() value = stmt.DB.NowFunc().UnixNano()
} else if field.AutoUpdateTime == schema.UnixMillisecond { } else if field.AutoUpdateTime == schema.UnixMillisecond {
@ -241,7 +240,6 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
} }
isZero = false isZero = false
} }
}
if ok || !isZero { if ok || !isZero {
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: value}) set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: value})

View File

@ -466,7 +466,9 @@ func TestSelectWithUpdateColumn(t *testing.T) {
var result2 User var result2 User
DB.First(&result2, user.ID) DB.First(&result2, user.ID)
AssertEqual(t, lastUpdatedAt, result2.UpdatedAt) if lastUpdatedAt.Format(time.RFC3339Nano) == result2.UpdatedAt.Format(time.RFC3339Nano) {
t.Errorf("UpdatedAt should be changed")
}
if result2.Name == user.Name || result2.Age != user.Age { if result2.Name == user.Name || result2.Age != user.Age {
t.Errorf("Should only update users with name column") t.Errorf("Should only update users with name column")