forked from mirror/gorm
Allow customize auto increment increment
This commit is contained in:
parent
6c0ee2700a
commit
79864af9ff
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue