Only set autoincrement if the tag is used.
This commit is contained in:
parent
bf413d67d3
commit
92821210e9
@ -32,28 +32,28 @@ func (mysql) DataTypeOf(field *StructField) string {
|
|||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
sqlType = "boolean"
|
sqlType = "boolean"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
||||||
sqlType = "int AUTO_INCREMENT"
|
sqlType = "int AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "int"
|
sqlType = "int"
|
||||||
}
|
}
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
||||||
sqlType = "int unsigned AUTO_INCREMENT"
|
sqlType = "int unsigned AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "int unsigned"
|
sqlType = "int unsigned"
|
||||||
}
|
}
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
||||||
sqlType = "bigint AUTO_INCREMENT"
|
sqlType = "bigint AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "bigint"
|
sqlType = "bigint"
|
||||||
}
|
}
|
||||||
case reflect.Uint64:
|
case reflect.Uint64:
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
||||||
sqlType = "bigint unsigned AUTO_INCREMENT"
|
sqlType = "bigint unsigned AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
|
@ -31,14 +31,14 @@ func (postgres) DataTypeOf(field *StructField) string {
|
|||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
sqlType = "boolean"
|
sqlType = "boolean"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
||||||
sqlType = "serial"
|
sqlType = "serial"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "integer"
|
sqlType = "integer"
|
||||||
}
|
}
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
||||||
sqlType = "bigserial"
|
sqlType = "bigserial"
|
||||||
} else {
|
} else {
|
||||||
|
@ -29,18 +29,19 @@ func (sqlite3) DataTypeOf(field *StructField) string {
|
|||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
sqlType = "bool"
|
sqlType = "bool"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||||
|
sqlType = "integer"
|
||||||
if field.IsPrimaryKey {
|
if field.IsPrimaryKey {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
sqlType += "primary key"
|
||||||
sqlType = "integer primary key autoincrement"
|
}
|
||||||
} else {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
sqlType = "integer"
|
sqlType += "autoincrement"
|
||||||
}
|
}
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
|
sqlType = "bigint"
|
||||||
if field.IsPrimaryKey {
|
if field.IsPrimaryKey {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
||||||
sqlType = "integer primary key autoincrement"
|
sqlType = "integer primary key autoincrement"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "bigint"
|
|
||||||
}
|
}
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
sqlType = "real"
|
sqlType = "real"
|
||||||
|
2
model.go
2
model.go
@ -7,7 +7,7 @@ import "time"
|
|||||||
// gorm.Model
|
// gorm.Model
|
||||||
// }
|
// }
|
||||||
type Model struct {
|
type Model struct {
|
||||||
ID uint `gorm:"primary_key"`
|
ID uint `gorm:"primary_key;AUTO_INCREMENT"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt *time.Time `sql:"index"`
|
DeletedAt *time.Time `sql:"index"`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user