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) _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv)
if isZero { if isZero {
db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID) db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
insertID-- insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
} }
} }
} else { } else {
@ -83,7 +83,7 @@ func Create(config *Config) func(db *gorm.DB) {
if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv); isZero { if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(rv); isZero {
db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID) 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 GORMDataType DataType
PrimaryKey bool PrimaryKey bool
AutoIncrement bool AutoIncrement bool
AutoIncrementIncrement int64
Creatable bool Creatable bool
Updatable bool Updatable bool
Readable bool Readable bool
@ -86,6 +87,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
Tag: fieldStruct.Tag, Tag: fieldStruct.Tag,
TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"), TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"),
Schema: schema, Schema: schema,
AutoIncrementIncrement: 1,
} }
for field.IndirectFieldType.Kind() == reflect.Ptr { for field.IndirectFieldType.Kind() == reflect.Ptr {
@ -149,6 +151,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field.HasDefaultValue = true field.HasDefaultValue = true
} }
if num, ok := field.TagSettings["AUTOINCREMENTINCREMENT"]; ok {
field.AutoIncrementIncrement, _ = strconv.ParseInt(num, 10, 64)
}
if v, ok := field.TagSettings["DEFAULT"]; ok { if v, ok := field.TagSettings["DEFAULT"]; ok {
field.HasDefaultValue = true field.HasDefaultValue = true
field.DefaultValue = v field.DefaultValue = v