Fix []byte type

This commit is contained in:
Jinzhu 2013-11-21 13:47:49 +08:00
parent 29bc814934
commit 0208ce7901
2 changed files with 18 additions and 6 deletions

View File

@ -47,7 +47,9 @@ func (f *Field) sqlTag() (str string) {
switch reflect_value.Kind() { switch reflect_value.Kind() {
case reflect.Slice: case reflect.Slice:
if _, ok := f.Value.([]byte); !ok {
return return
}
case reflect.Struct: case reflect.Struct:
if !f.isTime() && !f.isScanner() { if !f.isTime() && !f.isScanner() {
return return
@ -79,11 +81,13 @@ func (f *Field) parseAssociation() {
switch reflect_value.Kind() { switch reflect_value.Kind() {
case reflect.Slice: case reflect.Slice:
if _, ok := f.Value.([]byte); !ok {
foreign_key := f.model.typeName() + "Id" foreign_key := f.model.typeName() + "Id"
if reflect.New(reflect_value.Type().Elem()).Elem().FieldByName(foreign_key).IsValid() { if reflect.New(reflect_value.Type().Elem()).Elem().FieldByName(foreign_key).IsValid() {
f.foreignKey = foreign_key f.foreignKey = foreign_key
} }
f.afterAssociation = true f.afterAssociation = true
}
case reflect.Struct: case reflect.Struct:
if !f.isTime() && !f.isScanner() { if !f.isTime() && !f.isScanner() {
if f.model.reflectData().FieldByName(f.Name + "Id").IsValid() { if f.model.reflectData().FieldByName(f.Name + "Id").IsValid() {

View File

@ -28,6 +28,7 @@ type User struct {
ShippingAddress Address // Embedded struct ShippingAddress Address // Embedded struct
ShippingAddressId int64 // Embedded struct's foreign key ShippingAddressId int64 // Embedded struct's foreign key
CreditCard CreditCard CreditCard CreditCard
PasswordHash []byte
IgnoreMe int64 `sql:"-"` IgnoreMe int64 `sql:"-"`
} }
@ -81,6 +82,7 @@ var (
func init() { func init() {
var err error var err error
db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable") db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm'; // CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
// CREATE DATABASE 'gorm'; // CREATE DATABASE 'gorm';
@ -158,12 +160,18 @@ func TestFirstAndLast(t *testing.T) {
func TestCreateAndUpdate(t *testing.T) { func TestCreateAndUpdate(t *testing.T) {
name, name2, new_name := "update", "update2", "new_update" 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) db.Save(&user)
if user.Id == 0 { if user.Id == 0 {
t.Errorf("Should have ID after create") 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}) db.Save(&User{Name: name2, Age: 1})
user.Name = new_name user.Name = new_name