Set AUTO_INCREMENT if field is an auto increment field

This commit is contained in:
Jinzhu 2016-05-04 10:37:31 +08:00
parent 465f8ea05b
commit c669e4b791
3 changed files with 8 additions and 0 deletions

View File

@ -33,24 +33,28 @@ func (mysql) DataTypeOf(field *StructField) string {
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.IsPrimaryKey {
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.IsPrimaryKey {
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.IsPrimaryKey {
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.IsPrimaryKey {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "bigint unsigned AUTO_INCREMENT" sqlType = "bigint unsigned AUTO_INCREMENT"
} else { } else {
sqlType = "bigint unsigned" sqlType = "bigint unsigned"

View File

@ -32,12 +32,14 @@ func (postgres) DataTypeOf(field *StructField) string {
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.IsPrimaryKey {
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.IsPrimaryKey {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "bigserial" sqlType = "bigserial"
} else { } else {
sqlType = "bigint" sqlType = "bigint"

View File

@ -30,12 +30,14 @@ func (sqlite3) DataTypeOf(field *StructField) string {
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:
if field.IsPrimaryKey { if field.IsPrimaryKey {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "integer primary key autoincrement" sqlType = "integer primary key autoincrement"
} else { } else {
sqlType = "integer" sqlType = "integer"
} }
case reflect.Int64, reflect.Uint64: case reflect.Int64, reflect.Uint64:
if field.IsPrimaryKey { if field.IsPrimaryKey {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "integer primary key autoincrement" sqlType = "integer primary key autoincrement"
} else { } else {
sqlType = "bigint" sqlType = "bigint"