mirror of https://github.com/go-gorm/gorm.git
Add GORMDataType to Field, close #3171
This commit is contained in:
parent
5d05441067
commit
ef002fd7ac
|
@ -202,7 +202,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
|||
|
||||
if field.AutoUpdateTime == schema.UnixNanosecond {
|
||||
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})
|
||||
} else {
|
||||
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 == schema.UnixNanosecond {
|
||||
value = stmt.DB.NowFunc().UnixNano()
|
||||
} else if field.DataType == schema.Time {
|
||||
} else if field.GORMDataType == schema.Time {
|
||||
value = stmt.DB.NowFunc()
|
||||
} else {
|
||||
value = stmt.DB.NowFunc().Unix()
|
||||
|
|
1
gorm.go
1
gorm.go
|
@ -300,6 +300,7 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac
|
|||
for _, ref := range relation.References {
|
||||
if f := joinSchema.LookUpField(ref.ForeignKey.DBName); f != nil {
|
||||
f.DataType = ref.ForeignKey.DataType
|
||||
f.GORMDataType = ref.ForeignKey.GORMDataType
|
||||
ref.ForeignKey = f
|
||||
} else {
|
||||
return fmt.Errorf("missing field %v for join table", ref.ForeignKey.DBName)
|
||||
|
|
|
@ -38,6 +38,7 @@ type Field struct {
|
|||
DBName string
|
||||
BindNames []string
|
||||
DataType DataType
|
||||
GORMDataType DataType
|
||||
PrimaryKey bool
|
||||
AutoIncrement 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 {
|
||||
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 {
|
||||
switch reflect.Indirect(fieldValue).Kind() {
|
||||
case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.Float64:
|
||||
|
|
|
@ -157,6 +157,7 @@ func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Fi
|
|||
|
||||
// use same data type for foreign keys
|
||||
relation.Polymorphic.PolymorphicID.DataType = primaryKeyField.DataType
|
||||
relation.Polymorphic.PolymorphicID.GORMDataType = primaryKeyField.GORMDataType
|
||||
|
||||
relation.References = append(relation.References, &Reference{
|
||||
PrimaryKey: primaryKeyField,
|
||||
|
@ -285,6 +286,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
|||
for idx, f := range relation.JoinTable.Fields {
|
||||
// use same data type for foreign keys
|
||||
f.DataType = fieldsMap[f.Name].DataType
|
||||
f.GORMDataType = fieldsMap[f.Name].GORMDataType
|
||||
relation.JoinTable.PrimaryFields[idx] = f
|
||||
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 {
|
||||
// use same data type for foreign keys
|
||||
foreignField.DataType = primaryFields[idx].DataType
|
||||
foreignField.GORMDataType = primaryFields[idx].GORMDataType
|
||||
|
||||
relation.References = append(relation.References, &Reference{
|
||||
PrimaryKey: primaryFields[idx],
|
||||
|
|
|
@ -182,7 +182,7 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
|
|||
}
|
||||
|
||||
if field := schema.PrioritizedPrimaryField; field != nil {
|
||||
switch field.DataType {
|
||||
switch field.GORMDataType {
|
||||
case Int, Uint:
|
||||
if !field.HasDefaultValue || field.DefaultValueInterface != nil {
|
||||
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
|
||||
|
|
Loading…
Reference in New Issue