diff --git a/create_test.go b/create_test.go index dc82de50..2d71c9a6 100644 --- a/create_test.go +++ b/create_test.go @@ -57,6 +57,21 @@ 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{} + + DB.Create(&user1) + DB.Create(&user2) + + if user2.Sequence-user1.Sequence != 1 { + t.Errorf("Auto increment should apply on Sequence") + } +} + func TestCreateWithNoGORMPrimayKey(t *testing.T) { if dialect := os.Getenv("GORM_DIALECT"); dialect == "mssql" { t.Skip("Skipping this because MSSQL will return identity only if the table has an Id column") 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: diff --git a/migration_test.go b/migration_test.go index 592dadb7..ec33efc1 100644 --- a/migration_test.go +++ b/migration_test.go @@ -33,6 +33,7 @@ type User struct { Company Company Role PasswordHash []byte + Sequence uint `gorm:"AUTO_INCREMENT"` IgnoreMe int64 `sql:"-"` IgnoreStringSlice []string `sql:"-"` Ignored struct{ Name string } `sql:"-"` diff --git a/model_struct.go b/model_struct.go index eb3762f4..d8f9ed1b 100644 --- a/model_struct.go +++ b/model_struct.go @@ -175,6 +175,10 @@ func (scope *Scope) GetModelStruct() *ModelStruct { field.HasDefaultValue = true } + if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok { + field.HasDefaultValue = true + } + indirectType := fieldStruct.Type for indirectType.Kind() == reflect.Ptr { indirectType = indirectType.Elem() diff --git a/preload_test.go b/preload_test.go index da3ee38f..8c56a8ac 100644 --- a/preload_test.go +++ b/preload_test.go @@ -1509,6 +1509,88 @@ func TestNilPointerSlice2(t *testing.T) { } } +func TestPrefixedPreloadDuplication(t *testing.T) { + type ( + Level4 struct { + ID uint + Name string + Level3ID uint + } + Level3 struct { + ID uint + Name string + Level4s []*Level4 + } + Level2 struct { + ID uint + Name string + Level3ID sql.NullInt64 `sql:"index"` + Level3 *Level3 + } + Level1 struct { + ID uint + Name string + Level2ID sql.NullInt64 `sql:"index"` + Level2 *Level2 + } + ) + + DB.DropTableIfExists(new(Level3)) + DB.DropTableIfExists(new(Level4)) + DB.DropTableIfExists(new(Level2)) + DB.DropTableIfExists(new(Level1)) + + if err := DB.AutoMigrate(new(Level3), new(Level4), new(Level2), new(Level1)).Error; err != nil { + t.Error(err) + } + + lvl := &Level3{} + if err := DB.Save(lvl).Error; err != nil { + t.Error(err) + } + + sublvl1 := &Level4{Level3ID: lvl.ID} + if err := DB.Save(sublvl1).Error; err != nil { + t.Error(err) + } + sublvl2 := &Level4{Level3ID: lvl.ID} + if err := DB.Save(sublvl2).Error; err != nil { + t.Error(err) + } + + lvl.Level4s = []*Level4{sublvl1, sublvl2} + + want1 := Level1{ + Level2: &Level2{ + Level3: lvl, + }, + } + if err := DB.Save(&want1).Error; err != nil { + t.Error(err) + } + + want2 := Level1{ + Level2: &Level2{ + Level3: lvl, + }, + } + if err := DB.Save(&want2).Error; err != nil { + t.Error(err) + } + + want := []Level1{want1, want2} + + var got []Level1 + err := DB.Preload("Level2.Level3.Level4s").Find(&got).Error + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want)) + } +} + func toJSONString(v interface{}) []byte { r, _ := json.MarshalIndent(v, "", " ") return r diff --git a/scope.go b/scope.go index 0ecf43df..974ff035 100644 --- a/scope.go +++ b/scope.go @@ -1237,6 +1237,7 @@ func (scope *Scope) getColumnAsScope(column string) *Scope { fieldType = fieldType.Elem() } + resultsMap := map[interface{}]bool{} results := reflect.New(reflect.SliceOf(reflect.PtrTo(fieldType))).Elem() for i := 0; i < indirectScopeValue.Len(); i++ { @@ -1244,11 +1245,13 @@ func (scope *Scope) getColumnAsScope(column string) *Scope { if result.Kind() == reflect.Slice { for j := 0; j < result.Len(); j++ { - if elem := result.Index(j); elem.CanAddr() { + if elem := result.Index(j); elem.CanAddr() && resultsMap[elem.Addr()] != true { + resultsMap[elem.Addr()] = true results = reflect.Append(results, elem.Addr()) } } - } else if result.CanAddr() { + } else if result.CanAddr() && resultsMap[result.Addr()] != true { + resultsMap[result.Addr()] = true results = reflect.Append(results, result.Addr()) } }