Allow customize auto increment increment

This commit is contained in:
Jinzhu 2020-12-30 11:16:40 +08:00
parent 6c0ee2700a
commit 79864af9ff
2 changed files with 51 additions and 45 deletions

View File

@ -71,7 +71,7 @@ func Create(config *Config) func(db *gorm.DB) {
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv)
if isZero {
db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
insertID--
insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
}
}
} else {
@ -83,7 +83,7 @@ func Create(config *Config) func(db *gorm.DB) {
if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv); isZero {
db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
insertID++
insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
}
}
}

View File

@ -44,6 +44,7 @@ type Field struct {
GORMDataType DataType
PrimaryKey bool
AutoIncrement bool
AutoIncrementIncrement int64
Creatable bool
Updatable bool
Readable bool
@ -86,6 +87,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
Tag: fieldStruct.Tag,
TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"),
Schema: schema,
AutoIncrementIncrement: 1,
}
for field.IndirectFieldType.Kind() == reflect.Ptr {
@ -149,6 +151,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field.HasDefaultValue = true
}
if num, ok := field.TagSettings["AUTOINCREMENTINCREMENT"]; ok {
field.AutoIncrementIncrement, _ = strconv.ParseInt(num, 10, 64)
}
if v, ok := field.TagSettings["DEFAULT"]; ok {
field.HasDefaultValue = true
field.DefaultValue = v