Test AUTO_INCREMENT only on postgres

Only the postgres dialect handles AUTO_INCREMENT on non-primary key.
So we skip the auto increment test for other dialects.

The mysql case is a little trickier because the simple presence of the
'AUTH_INCREMENT' tag produces a faulty 'CREATE TABLE' statement. Hence
we need to remove it when present.
This commit is contained in:
Christophe de Vienne 2016-06-20 16:00:38 +02:00
parent 608fd976c4
commit 328fe672c8
2 changed files with 11 additions and 0 deletions

View File

@ -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{}

View File

@ -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: