Adds processing of ignored fields on Update

This commit is contained in:
elgris 2016-07-12 10:10:18 +03:00 committed by Jinzhu
parent 34e75afb42
commit fb09befb9b
3 changed files with 33 additions and 2 deletions

View File

@ -252,7 +252,7 @@ func runMigration() {
DB.Exec(fmt.Sprintf("drop table %v;", table))
}
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Toy{}}
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Toy{}, &ElementWithIgnoredField{}}
for _, value := range values {
DB.DropTable(value)
}

View File

@ -835,7 +835,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
}
default:
for _, field := range (&Scope{Value: values}).Fields() {
if !field.IsBlank {
if !field.IsBlank && !field.IsIgnored {
attrs[field.DBName] = field.Field.Interface()
}
}

View File

@ -419,6 +419,37 @@ func TestUpdatesWithBlankValues(t *testing.T) {
}
}
type ElementWithIgnoredField struct {
Id int64
Value string
IgnoredField int64 `sql:"-"`
}
func (e ElementWithIgnoredField) TableName() string {
return "element_with_ignored_field"
}
func TestUpdatesTableWithIgnoredValues(t *testing.T) {
elem := ElementWithIgnoredField{Value: "foo", IgnoredField: 10}
DB.LogMode(true)
DB.Save(&elem)
DB.Table(elem.TableName()).
Where("id = ?", elem.Id).
// DB.Model(&ElementWithIgnoredField{Id: elem.Id}).
Updates(&ElementWithIgnoredField{Value: "bar", IgnoredField: 100})
var elem1 ElementWithIgnoredField
err := DB.First(&elem1, elem.Id).Error
if err != nil {
t.Errorf("error getting an element from database: %s", err.Error())
}
if elem1.IgnoredField != 0 {
t.Errorf("element's ignored field should not be updated")
}
}
func TestUpdateDecodeVirtualAttributes(t *testing.T) {
var user = User{
Name: "jinzhu",