From c669e4b791c17955380c771e164ce9899b8efc70 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 4 May 2016 10:37:31 +0800 Subject: [PATCH] Set AUTO_INCREMENT if field is an auto increment field --- dialect_mysql.go | 4 ++++ dialect_postgres.go | 2 ++ dialect_sqlite3.go | 2 ++ 3 files changed, 8 insertions(+) diff --git a/dialect_mysql.go b/dialect_mysql.go index 6fade59d..f62bbe35 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -33,24 +33,28 @@ func (mysql) DataTypeOf(field *StructField) string { sqlType = "boolean" case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "int AUTO_INCREMENT" } else { sqlType = "int" } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "int unsigned AUTO_INCREMENT" } else { sqlType = "int unsigned" } case reflect.Int64: if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "bigint AUTO_INCREMENT" } else { sqlType = "bigint" } case reflect.Uint64: if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "bigint unsigned AUTO_INCREMENT" } else { sqlType = "bigint unsigned" diff --git a/dialect_postgres.go b/dialect_postgres.go index 09ac5961..c5bed8f1 100644 --- a/dialect_postgres.go +++ b/dialect_postgres.go @@ -32,12 +32,14 @@ func (postgres) DataTypeOf(field *StructField) string { sqlType = "boolean" 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 { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "serial" } else { sqlType = "integer" } case reflect.Int64, reflect.Uint64: if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "bigserial" } else { sqlType = "bigint" diff --git a/dialect_sqlite3.go b/dialect_sqlite3.go index 5c262aaf..79adf6d2 100644 --- a/dialect_sqlite3.go +++ b/dialect_sqlite3.go @@ -30,12 +30,14 @@ func (sqlite3) DataTypeOf(field *StructField) string { sqlType = "bool" case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: if field.IsPrimaryKey { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "integer primary key autoincrement" } else { sqlType = "integer" } case reflect.Int64, reflect.Uint64: if field.IsPrimaryKey { + field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT" sqlType = "integer primary key autoincrement" } else { sqlType = "bigint"