mirror of https://github.com/go-gorm/gorm.git
Fix AutoMigrate for bool fields with default value
This commit is contained in:
parent
e0c3be03fb
commit
1b0aa802df
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -518,13 +519,19 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
|
||||||
} else if !dvNotNull && currentDefaultNotNull {
|
} else if !dvNotNull && currentDefaultNotNull {
|
||||||
// null -> default value
|
// null -> default value
|
||||||
alterColumn = true
|
alterColumn = true
|
||||||
} else if (field.GORMDataType != schema.Time && dv != field.DefaultValue) ||
|
} else if currentDefaultNotNull || dvNotNull {
|
||||||
(field.GORMDataType == schema.Time && !strings.EqualFold(strings.TrimSuffix(dv, "()"), strings.TrimSuffix(field.DefaultValue, "()"))) {
|
switch field.GORMDataType {
|
||||||
// default value not equal
|
case schema.Time:
|
||||||
// not both null
|
if !strings.EqualFold(strings.TrimSuffix(dv, "()"), strings.TrimSuffix(field.DefaultValue, "()")) {
|
||||||
if currentDefaultNotNull || dvNotNull {
|
|
||||||
alterColumn = true
|
alterColumn = true
|
||||||
}
|
}
|
||||||
|
case schema.Bool:
|
||||||
|
v1, _ := strconv.ParseBool(dv)
|
||||||
|
v2, _ := strconv.ParseBool(field.DefaultValue)
|
||||||
|
alterColumn = v1 != v2
|
||||||
|
default:
|
||||||
|
alterColumn = dv != field.DefaultValue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -1420,7 +1421,7 @@ func TestMigrateSameEmbeddedFieldName(t *testing.T) {
|
||||||
AssertEqual(t, nil, err)
|
AssertEqual(t, nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMigrateDefaultNullString(t *testing.T) {
|
func TestMigrateWithDefaultValue(t *testing.T) {
|
||||||
if DB.Dialector.Name() == "sqlserver" {
|
if DB.Dialector.Name() == "sqlserver" {
|
||||||
// sqlserver driver treats NULL and 'NULL' the same
|
// sqlserver driver treats NULL and 'NULL' the same
|
||||||
t.Skip("skip sqlserver")
|
t.Skip("skip sqlserver")
|
||||||
|
@ -1434,6 +1435,7 @@ func TestMigrateDefaultNullString(t *testing.T) {
|
||||||
type NullStringModel struct {
|
type NullStringModel struct {
|
||||||
ID uint
|
ID uint
|
||||||
Content string `gorm:"default:'null'"`
|
Content string `gorm:"default:'null'"`
|
||||||
|
Active bool `gorm:"default:false"`
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName := "null_string_model"
|
tableName := "null_string_model"
|
||||||
|
@ -1454,6 +1456,14 @@ func TestMigrateDefaultNullString(t *testing.T) {
|
||||||
AssertEqual(t, defVal, "null")
|
AssertEqual(t, defVal, "null")
|
||||||
AssertEqual(t, ok, true)
|
AssertEqual(t, ok, true)
|
||||||
|
|
||||||
|
columnType2, err := findColumnType(tableName, "active")
|
||||||
|
AssertEqual(t, err, nil)
|
||||||
|
|
||||||
|
defVal, ok = columnType2.DefaultValue()
|
||||||
|
bv, _ := strconv.ParseBool(defVal)
|
||||||
|
AssertEqual(t, bv, false)
|
||||||
|
AssertEqual(t, ok, true)
|
||||||
|
|
||||||
// default 'null' -> 'null'
|
// default 'null' -> 'null'
|
||||||
session := DB.Session(&gorm.Session{Logger: Tracer{
|
session := DB.Session(&gorm.Session{Logger: Tracer{
|
||||||
Logger: DB.Config.Logger,
|
Logger: DB.Config.Logger,
|
||||||
|
|
Loading…
Reference in New Issue