From 1b28c187c0374e3a1347221fece12d8d8d5e40c0 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 25 Jun 2020 08:00:10 +0800 Subject: [PATCH] Fix create with default value --- migrator/migrator.go | 8 ++++---- tests/default_value_test.go | 13 +++++++------ tests/go.mod | 2 ++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index 799bf433..9c4ce2d5 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -65,10 +65,10 @@ func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr) { } if field.HasDefaultValue && field.DefaultValue != "" { - if field.DataType == schema.String && field.DefaultValueInterface != nil { - defaultStmt := &gorm.Statement{Vars: []interface{}{field.DefaultValue}} - m.Dialector.BindVarTo(defaultStmt, defaultStmt, field.DefaultValue) - expr.SQL += " DEFAULT " + m.Dialector.Explain(defaultStmt.SQL.String(), field.DefaultValue) + if field.DefaultValueInterface != nil { + defaultStmt := &gorm.Statement{Vars: []interface{}{field.DefaultValueInterface}} + m.Dialector.BindVarTo(defaultStmt, defaultStmt, field.DefaultValueInterface) + expr.SQL += " DEFAULT " + m.Dialector.Explain(defaultStmt.SQL.String(), field.DefaultValueInterface) } else { expr.SQL += " DEFAULT " + field.DefaultValue } diff --git a/tests/default_value_test.go b/tests/default_value_test.go index 52292cf7..28a456d3 100644 --- a/tests/default_value_test.go +++ b/tests/default_value_test.go @@ -9,10 +9,11 @@ import ( func TestDefaultValue(t *testing.T) { type Harumph struct { gorm.Model - Email string `gorm:"not null;"` - Name string `gorm:"not null;default:foo"` - Name2 string `gorm:"not null;default:'foo'"` - Age int `gorm:"default:18"` + Email string `gorm:"not null;index:,unique"` + Name string `gorm:"not null;default:'foo'"` + Name2 string `gorm:"not null;default:'foo'"` + Age int `gorm:"default:18"` + Enabled bool `gorm:"default:true"` } DB.Migrator().DropTable(&Harumph{}) @@ -24,14 +25,14 @@ func TestDefaultValue(t *testing.T) { var harumph = Harumph{Email: "hello@gorm.io"} if err := DB.Create(&harumph).Error; err != nil { t.Fatalf("Failed to create data with default value, got error: %v", err) - } else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Age != 18 { + } else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Age != 18 || !harumph.Enabled { t.Fatalf("Failed to create data with default value, got: %+v", harumph) } var result Harumph if err := DB.First(&result, "email = ?", "hello@gorm.io").Error; err != nil { t.Fatalf("Failed to find created data, got error: %v", err) - } else if result.Name != "foo" || result.Name2 != "foo" || result.Age != 18 { + } else if result.Name != "foo" || result.Name2 != "foo" || result.Age != 18 || !result.Enabled { t.Fatalf("Failed to find created data with default data, got %+v", result) } } diff --git a/tests/go.mod b/tests/go.mod index f4d93ecb..d43ee8f1 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -14,3 +14,5 @@ require ( ) replace gorm.io/gorm => ../ + +replace gorm.io/driver/sqlserver => /Users/jinzhu/Projects/sqlserver