forked from mirror/gorm
Merge pull request #1075 from cdevienne/fix_autoincrement
Fix autoincrement
This commit is contained in:
commit
3324ab2063
|
@ -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) {
|
func TestCreateWithNoGORMPrimayKey(t *testing.T) {
|
||||||
if dialect := os.Getenv("GORM_DIALECT"); dialect == "mssql" {
|
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")
|
t.Skip("Skipping this because MSSQL will return identity only if the table has an Id column")
|
||||||
|
|
|
@ -30,6 +30,14 @@ func (mysql) Quote(key string) string {
|
||||||
func (mysql) DataTypeOf(field *StructField) string {
|
func (mysql) DataTypeOf(field *StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
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 == "" {
|
if sqlType == "" {
|
||||||
switch dataValue.Kind() {
|
switch dataValue.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
|
|
|
@ -33,6 +33,7 @@ type User struct {
|
||||||
Company Company
|
Company Company
|
||||||
Role
|
Role
|
||||||
PasswordHash []byte
|
PasswordHash []byte
|
||||||
|
Sequence uint `gorm:"AUTO_INCREMENT"`
|
||||||
IgnoreMe int64 `sql:"-"`
|
IgnoreMe int64 `sql:"-"`
|
||||||
IgnoreStringSlice []string `sql:"-"`
|
IgnoreStringSlice []string `sql:"-"`
|
||||||
Ignored struct{ Name string } `sql:"-"`
|
Ignored struct{ Name string } `sql:"-"`
|
||||||
|
|
|
@ -175,6 +175,10 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
field.HasDefaultValue = true
|
field.HasDefaultValue = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
|
field.HasDefaultValue = true
|
||||||
|
}
|
||||||
|
|
||||||
indirectType := fieldStruct.Type
|
indirectType := fieldStruct.Type
|
||||||
for indirectType.Kind() == reflect.Ptr {
|
for indirectType.Kind() == reflect.Ptr {
|
||||||
indirectType = indirectType.Elem()
|
indirectType = indirectType.Elem()
|
||||||
|
|
Loading…
Reference in New Issue