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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,55 +37,57 @@ const (
|
|||
)
|
||||
|
||||
type Field struct {
|
||||
Name string
|
||||
DBName string
|
||||
BindNames []string
|
||||
DataType DataType
|
||||
GORMDataType DataType
|
||||
PrimaryKey bool
|
||||
AutoIncrement bool
|
||||
Creatable bool
|
||||
Updatable bool
|
||||
Readable bool
|
||||
HasDefaultValue bool
|
||||
AutoCreateTime TimeType
|
||||
AutoUpdateTime TimeType
|
||||
DefaultValue string
|
||||
DefaultValueInterface interface{}
|
||||
NotNull bool
|
||||
Unique bool
|
||||
Comment string
|
||||
Size int
|
||||
Precision int
|
||||
Scale int
|
||||
FieldType reflect.Type
|
||||
IndirectFieldType reflect.Type
|
||||
StructField reflect.StructField
|
||||
Tag reflect.StructTag
|
||||
TagSettings map[string]string
|
||||
Schema *Schema
|
||||
EmbeddedSchema *Schema
|
||||
OwnerSchema *Schema
|
||||
ReflectValueOf func(reflect.Value) reflect.Value
|
||||
ValueOf func(reflect.Value) (value interface{}, zero bool)
|
||||
Set func(reflect.Value, interface{}) error
|
||||
Name string
|
||||
DBName string
|
||||
BindNames []string
|
||||
DataType DataType
|
||||
GORMDataType DataType
|
||||
PrimaryKey bool
|
||||
AutoIncrement bool
|
||||
AutoIncrementIncrement int64
|
||||
Creatable bool
|
||||
Updatable bool
|
||||
Readable bool
|
||||
HasDefaultValue bool
|
||||
AutoCreateTime TimeType
|
||||
AutoUpdateTime TimeType
|
||||
DefaultValue string
|
||||
DefaultValueInterface interface{}
|
||||
NotNull bool
|
||||
Unique bool
|
||||
Comment string
|
||||
Size int
|
||||
Precision int
|
||||
Scale int
|
||||
FieldType reflect.Type
|
||||
IndirectFieldType reflect.Type
|
||||
StructField reflect.StructField
|
||||
Tag reflect.StructTag
|
||||
TagSettings map[string]string
|
||||
Schema *Schema
|
||||
EmbeddedSchema *Schema
|
||||
OwnerSchema *Schema
|
||||
ReflectValueOf func(reflect.Value) reflect.Value
|
||||
ValueOf func(reflect.Value) (value interface{}, zero bool)
|
||||
Set func(reflect.Value, interface{}) error
|
||||
}
|
||||
|
||||
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
||||
var err error
|
||||
|
||||
field := &Field{
|
||||
Name: fieldStruct.Name,
|
||||
BindNames: []string{fieldStruct.Name},
|
||||
FieldType: fieldStruct.Type,
|
||||
IndirectFieldType: fieldStruct.Type,
|
||||
StructField: fieldStruct,
|
||||
Creatable: true,
|
||||
Updatable: true,
|
||||
Readable: true,
|
||||
Tag: fieldStruct.Tag,
|
||||
TagSettings: ParseTagSetting(fieldStruct.Tag.Get("gorm"), ";"),
|
||||
Schema: schema,
|
||||
Name: fieldStruct.Name,
|
||||
BindNames: []string{fieldStruct.Name},
|
||||
FieldType: fieldStruct.Type,
|
||||
IndirectFieldType: fieldStruct.Type,
|
||||
StructField: fieldStruct,
|
||||
Creatable: true,
|
||||
Updatable: true,
|
||||
Readable: true,
|
||||
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