Skip to content

Commit

Permalink
Test AUTO_INCREMENT only on postgres
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdevienne committed Jun 20, 2016
1 parent 608fd97 commit 328fe67
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
8 changes: 8 additions & 0 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 328fe67

Please sign in to comment.