diff --git a/create_test.go b/create_test.go index 28a049f4..2d71c9a6 100644 --- a/create_test.go +++ b/create_test.go @@ -58,6 +58,9 @@ func TestCreate(t *testing.T) { } func TestCreateWithAutoIncrement(t *testing.T) { + if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" { + t.Skip("Skipping this because only postgres properly support auto_increment on a non-primary_key column") + } user1 := User{} user2 := User{} diff --git a/dialect_mysql.go b/dialect_mysql.go index bc4828de..0ddcea4d 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -30,6 +30,14 @@ func (mysql) Quote(key string) string { func (mysql) DataTypeOf(field *StructField) string { var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field) + // MySQL allows only one auto increment column per table, and it must + // be a KEY column. + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok { + if _, ok = field.TagSettings["INDEX"]; !ok && !field.IsPrimaryKey { + delete(field.TagSettings, "AUTO_INCREMENT") + } + } + if sqlType == "" { switch dataValue.Kind() { case reflect.Bool: