mirror of https://github.com/go-gorm/gorm.git
Try to add support for NullInt64
This commit is contained in:
parent
99b87315c4
commit
562bca71e4
1
chain.go
1
chain.go
|
@ -70,6 +70,7 @@ func (s *Chain) do(value interface{}) *Do {
|
||||||
do.specifiedTableName = s.specifiedTableName
|
do.specifiedTableName = s.specifiedTableName
|
||||||
do.unscoped = s.unscoped
|
do.unscoped = s.unscoped
|
||||||
do.singularTableName = s.singularTableName
|
do.singularTableName = s.singularTableName
|
||||||
|
do.debug = s.debug
|
||||||
|
|
||||||
s.value = value
|
s.value = value
|
||||||
do.setModel(value)
|
do.setModel(value)
|
||||||
|
|
1
do.go
1
do.go
|
@ -17,6 +17,7 @@ type Do struct {
|
||||||
driver string
|
driver string
|
||||||
guessedTableName string
|
guessedTableName string
|
||||||
specifiedTableName string
|
specifiedTableName string
|
||||||
|
debug bool
|
||||||
Errors []error
|
Errors []error
|
||||||
|
|
||||||
model *Model
|
model *Model
|
||||||
|
|
|
@ -1081,7 +1081,7 @@ type Category struct {
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
Id int64
|
Id int64
|
||||||
CategoryId int64
|
CategoryId sql.NullInt64
|
||||||
MainCategoryId int64
|
MainCategoryId int64
|
||||||
Title string
|
Title string
|
||||||
Body string
|
Body string
|
||||||
|
@ -1124,7 +1124,7 @@ func TestSubStruct(t *testing.T) {
|
||||||
|
|
||||||
var p Post
|
var p Post
|
||||||
db.First(&p, post.Id)
|
db.First(&p, post.Id)
|
||||||
if post.CategoryId == 0 || p.CategoryId == 0 || post.MainCategoryId == 0 || p.MainCategoryId == 0 {
|
if post.CategoryId.Int64 == 0 || p.CategoryId.Int64 == 0 || post.MainCategoryId == 0 || p.MainCategoryId == 0 {
|
||||||
t.Errorf("Category Id should exist")
|
t.Errorf("Category Id should exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
38
model.go
38
model.go
|
@ -1,6 +1,7 @@
|
||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
@ -153,15 +154,19 @@ func (m *Model) fields(operation string) (fields []Field) {
|
||||||
if is_time {
|
if is_time {
|
||||||
field.SqlType = getSqlType(m.driver, field.Value, 0)
|
field.SqlType = getSqlType(m.driver, field.Value, 0)
|
||||||
} else {
|
} else {
|
||||||
if indirect_value.FieldByName(p.Name + "Id").IsValid() {
|
switch value.Interface().(type) {
|
||||||
field.foreignKey = p.Name + "Id"
|
case sql.NullInt64, sql.NullFloat64, sql.NullBool, sql.NullString:
|
||||||
field.beforeAssociation = true
|
default:
|
||||||
} else {
|
if indirect_value.FieldByName(p.Name + "Id").IsValid() {
|
||||||
foreign_key := typ.Name() + "Id"
|
field.foreignKey = p.Name + "Id"
|
||||||
if reflect.New(field_value.Type()).Elem().FieldByName(foreign_key).IsValid() {
|
field.beforeAssociation = true
|
||||||
field.foreignKey = foreign_key
|
} else {
|
||||||
|
foreign_key := typ.Name() + "Id"
|
||||||
|
if reflect.New(field_value.Type()).Elem().FieldByName(foreign_key).IsValid() {
|
||||||
|
field.foreignKey = foreign_key
|
||||||
|
}
|
||||||
|
field.afterAssociation = true
|
||||||
}
|
}
|
||||||
field.afterAssociation = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
|
@ -363,7 +368,22 @@ func setFieldValue(field reflect.Value, value interface{}) bool {
|
||||||
}
|
}
|
||||||
field.SetInt(reflect.ValueOf(value).Int())
|
field.SetInt(reflect.ValueOf(value).Int())
|
||||||
default:
|
default:
|
||||||
field.Set(reflect.ValueOf(value))
|
field_type := field.Type()
|
||||||
|
if field_type == reflect.TypeOf(value) {
|
||||||
|
field.Set(reflect.ValueOf(value))
|
||||||
|
} else if value == nil {
|
||||||
|
field.Set(reflect.Zero(field.Type()))
|
||||||
|
} else if field_type == reflect.TypeOf(sql.NullBool{}) {
|
||||||
|
field.Set(reflect.ValueOf(sql.NullBool{value.(bool), true}))
|
||||||
|
} else if field_type == reflect.TypeOf(sql.NullFloat64{}) {
|
||||||
|
field.Set(reflect.ValueOf(sql.NullFloat64{value.(float64), true}))
|
||||||
|
} else if field_type == reflect.TypeOf(sql.NullInt64{}) {
|
||||||
|
field.Set(reflect.ValueOf(sql.NullInt64{value.(int64), true}))
|
||||||
|
} else if field_type == reflect.TypeOf(sql.NullString{}) {
|
||||||
|
field.Set(reflect.ValueOf(sql.NullString{value.(string), true}))
|
||||||
|
} else {
|
||||||
|
field.Set(reflect.ValueOf(value))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue