Add field tag to ignore migration

This commit is contained in:
yrong1997 2021-01-31 13:39:00 +08:00
parent 7598204dc3
commit bce40e6d6a
3 changed files with 7 additions and 1 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ TODO*
documents
coverage.txt
_book
.idea

View File

@ -396,7 +396,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
}
}
if alterColumn {
if alterColumn && !field.IgnoreMigration {
return m.DB.Migrator().AlterColumn(value, field.Name)
}

View File

@ -70,6 +70,7 @@ type Field struct {
ReflectValueOf func(reflect.Value) reflect.Value
ValueOf func(reflect.Value) (value interface{}, zero bool)
Set func(reflect.Value, interface{}) error
IgnoreMigration bool
}
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
@ -188,6 +189,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field.Comment = val
}
if _, ok := field.TagSettings["IGNOREMIGRATION"]; ok {
field.IgnoreMigration = true
}
// default value is function or null or blank (primary keys)
skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") &&
strings.Contains(field.DefaultValue, ")") || strings.ToLower(field.DefaultValue) == "null" || field.DefaultValue == ""