diff --git a/field.go b/field.go index 6015a815..6c008270 100644 --- a/field.go +++ b/field.go @@ -47,7 +47,9 @@ func (f *Field) sqlTag() (str string) { switch reflect_value.Kind() { case reflect.Slice: - return + if _, ok := f.Value.([]byte); !ok { + return + } case reflect.Struct: if !f.isTime() && !f.isScanner() { return @@ -79,11 +81,13 @@ func (f *Field) parseAssociation() { switch reflect_value.Kind() { case reflect.Slice: - foreign_key := f.model.typeName() + "Id" - if reflect.New(reflect_value.Type().Elem()).Elem().FieldByName(foreign_key).IsValid() { - f.foreignKey = foreign_key + if _, ok := f.Value.([]byte); !ok { + foreign_key := f.model.typeName() + "Id" + if reflect.New(reflect_value.Type().Elem()).Elem().FieldByName(foreign_key).IsValid() { + f.foreignKey = foreign_key + } + f.afterAssociation = true } - f.afterAssociation = true case reflect.Struct: if !f.isTime() && !f.isScanner() { if f.model.reflectData().FieldByName(f.Name + "Id").IsValid() { diff --git a/gorm_test.go b/gorm_test.go index 5d4f030c..37571f5a 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -28,6 +28,7 @@ type User struct { ShippingAddress Address // Embedded struct ShippingAddressId int64 // Embedded struct's foreign key CreditCard CreditCard + PasswordHash []byte IgnoreMe int64 `sql:"-"` } @@ -81,6 +82,7 @@ var ( func init() { var err error + db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable") // CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm'; // CREATE DATABASE 'gorm'; @@ -158,12 +160,18 @@ func TestFirstAndLast(t *testing.T) { func TestCreateAndUpdate(t *testing.T) { name, name2, new_name := "update", "update2", "new_update" - user := User{Name: name, Age: 1} + user := User{Name: name, Age: 1, PasswordHash: []byte{'f', 'a', 'k', '4'}} db.Save(&user) if user.Id == 0 { t.Errorf("Should have ID after create") } + var u User + db.First(&u, user.Id) + if !reflect.DeepEqual(u.PasswordHash, []byte{'f', 'a', 'k', '4'}) { + t.Errorf("User's Password should be saved") + } + db.Save(&User{Name: name2, Age: 1}) user.Name = new_name