diff --git a/model_struct.go b/model_struct.go index 6022cf74..d4a46784 100644 --- a/model_struct.go +++ b/model_struct.go @@ -194,7 +194,9 @@ func (scope *Scope) GetModelStruct() *ModelStruct { if indirectType.Kind() == reflect.Struct { for i := 0; i < indirectType.NumField(); i++ { for key, value := range parseTagSetting(indirectType.Field(i).Tag) { - field.TagSettings[key] = value + if _, ok := field.TagSettings[key]; !ok { + field.TagSettings[key] = value + } } } } diff --git a/scaner_test.go b/scaner_test.go index cd89ca49..fae9d3e1 100644 --- a/scaner_test.go +++ b/scaner_test.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "testing" + + "github.com/jinzhu/gorm" ) func TestScannableSlices(t *testing.T) { @@ -83,3 +85,53 @@ func (l *ExampleStructSlice) Scan(input interface{}) error { return errors.New("not supported") } } + +type ScannerDataType struct { + Street string `sql:"TYPE:varchar(24)"` +} + +func (ScannerDataType) Value() (driver.Value, error) { + return nil, nil +} + +func (*ScannerDataType) Scan(input interface{}) error { + return nil +} + +type ScannerDataTypeTestStruct struct { + Field1 int + ScannerDataType *ScannerDataType `sql:"TYPE:json"` +} + +type ScannerDataType2 struct { + Street string `sql:"TYPE:varchar(24)"` +} + +func (ScannerDataType2) Value() (driver.Value, error) { + return nil, nil +} + +func (*ScannerDataType2) Scan(input interface{}) error { + return nil +} + +type ScannerDataTypeTestStruct2 struct { + Field1 int + ScannerDataType *ScannerDataType2 +} + +func TestScannerDataType(t *testing.T) { + scope := gorm.Scope{Value: &ScannerDataTypeTestStruct{}} + if field, ok := scope.FieldByName("ScannerDataType"); ok { + if DB.Dialect().DataTypeOf(field.StructField) != "json" { + t.Errorf("data type for scanner is wrong") + } + } + + scope = gorm.Scope{Value: &ScannerDataTypeTestStruct2{}} + if field, ok := scope.FieldByName("ScannerDataType"); ok { + if DB.Dialect().DataTypeOf(field.StructField) != "varchar(24)" { + t.Errorf("data type for scanner is wrong") + } + } +}