Add GORMDataType to Field, close #3171

This commit is contained in:
Jinzhu 2020-07-20 18:59:28 +08:00
parent 5d05441067
commit ef002fd7ac
5 changed files with 14 additions and 3 deletions

View File

@ -202,7 +202,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
if field.AutoUpdateTime == schema.UnixNanosecond { if field.AutoUpdateTime == schema.UnixNanosecond {
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.UnixNano()}) set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.UnixNano()})
} else if field.DataType == schema.Time { } else if field.GORMDataType == schema.Time {
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now}) set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now})
} else { } else {
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.Unix()}) set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.Unix()})
@ -223,7 +223,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
if field.AutoUpdateTime > 0 { if field.AutoUpdateTime > 0 {
if field.AutoUpdateTime == schema.UnixNanosecond { if field.AutoUpdateTime == schema.UnixNanosecond {
value = stmt.DB.NowFunc().UnixNano() value = stmt.DB.NowFunc().UnixNano()
} else if field.DataType == schema.Time { } else if field.GORMDataType == schema.Time {
value = stmt.DB.NowFunc() value = stmt.DB.NowFunc()
} else { } else {
value = stmt.DB.NowFunc().Unix() value = stmt.DB.NowFunc().Unix()

View File

@ -300,6 +300,7 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac
for _, ref := range relation.References { for _, ref := range relation.References {
if f := joinSchema.LookUpField(ref.ForeignKey.DBName); f != nil { if f := joinSchema.LookUpField(ref.ForeignKey.DBName); f != nil {
f.DataType = ref.ForeignKey.DataType f.DataType = ref.ForeignKey.DataType
f.GORMDataType = ref.ForeignKey.GORMDataType
ref.ForeignKey = f ref.ForeignKey = f
} else { } else {
return fmt.Errorf("missing field %v for join table", ref.ForeignKey.DBName) return fmt.Errorf("missing field %v for join table", ref.ForeignKey.DBName)

View File

@ -38,6 +38,7 @@ type Field struct {
DBName string DBName string
BindNames []string BindNames []string
DataType DataType DataType DataType
GORMDataType DataType
PrimaryKey bool PrimaryKey bool
AutoIncrement bool AutoIncrement bool
Creatable bool Creatable bool
@ -221,6 +222,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
} }
} }
field.GORMDataType = field.DataType
if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok { if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok {
field.DataType = DataType(dataTyper.GormDataType()) field.DataType = DataType(dataTyper.GormDataType())
} }
@ -250,6 +253,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
} }
} }
if field.GORMDataType == "" {
field.GORMDataType = field.DataType
}
if field.Size == 0 { if field.Size == 0 {
switch reflect.Indirect(fieldValue).Kind() { switch reflect.Indirect(fieldValue).Kind() {
case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.Float64: case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.Float64:

View File

@ -157,6 +157,7 @@ func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Fi
// use same data type for foreign keys // use same data type for foreign keys
relation.Polymorphic.PolymorphicID.DataType = primaryKeyField.DataType relation.Polymorphic.PolymorphicID.DataType = primaryKeyField.DataType
relation.Polymorphic.PolymorphicID.GORMDataType = primaryKeyField.GORMDataType
relation.References = append(relation.References, &Reference{ relation.References = append(relation.References, &Reference{
PrimaryKey: primaryKeyField, PrimaryKey: primaryKeyField,
@ -285,6 +286,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
for idx, f := range relation.JoinTable.Fields { for idx, f := range relation.JoinTable.Fields {
// use same data type for foreign keys // use same data type for foreign keys
f.DataType = fieldsMap[f.Name].DataType f.DataType = fieldsMap[f.Name].DataType
f.GORMDataType = fieldsMap[f.Name].GORMDataType
relation.JoinTable.PrimaryFields[idx] = f relation.JoinTable.PrimaryFields[idx] = f
ownPriamryField := schema == fieldsMap[f.Name].Schema && ownFieldsMap[f.Name] ownPriamryField := schema == fieldsMap[f.Name].Schema && ownFieldsMap[f.Name]
@ -387,6 +389,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, guessH
for idx, foreignField := range foreignFields { for idx, foreignField := range foreignFields {
// use same data type for foreign keys // use same data type for foreign keys
foreignField.DataType = primaryFields[idx].DataType foreignField.DataType = primaryFields[idx].DataType
foreignField.GORMDataType = primaryFields[idx].GORMDataType
relation.References = append(relation.References, &Reference{ relation.References = append(relation.References, &Reference{
PrimaryKey: primaryFields[idx], PrimaryKey: primaryFields[idx],

View File

@ -182,7 +182,7 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
} }
if field := schema.PrioritizedPrimaryField; field != nil { if field := schema.PrioritizedPrimaryField; field != nil {
switch field.DataType { switch field.GORMDataType {
case Int, Uint: case Int, Uint:
if !field.HasDefaultValue || field.DefaultValueInterface != nil { if !field.HasDefaultValue || field.DefaultValueInterface != nil {
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field) schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)