Skip to content

Commit

Permalink
Fix auto_increment on postgres database.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdevienne committed Jun 20, 2016
1 parent caa7926 commit 608fd97
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func TestCreate(t *testing.T) {
}
}

func TestCreateWithAutoIncrement(t *testing.T) {
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")
Expand Down
1 change: 1 addition & 0 deletions migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down
4 changes: 4 additions & 0 deletions model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 608fd97

Please sign in to comment.