Skip to content

Commit

Permalink
fix: sqlite dialector cannot apply PRIMARY KEY AUTOINCREMENT type (#…
Browse files Browse the repository at this point in the history
…6624)

* fix: sqlite dialector cannot apply `PRIMARY KEY AUTOINCREMENT` type

fix #4760

* feat: add auto increment test

* feat: update sqlite

* feat: update tests deps sqlite to v1.5.4
  • Loading branch information
samuelncui authored Oct 9, 2023
1 parent e57e5d8 commit 2095d42
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (m Migrator) CreateTable(values ...interface{}) error {
field := stmt.Schema.FieldsByDBName[dbName]
if !field.IgnoreMigration {
createTableSQL += "? ?"
hasPrimaryKeyInDataType = hasPrimaryKeyInDataType || strings.Contains(strings.ToUpper(string(field.DataType)), "PRIMARY KEY")
hasPrimaryKeyInDataType = hasPrimaryKeyInDataType || strings.Contains(strings.ToUpper(m.DataTypeOf(field)), "PRIMARY KEY")
values = append(values, clause.Column{Name: dbName}, m.DB.Migrator().FullDataTypeOf(field))
createTableSQL += ","
}
Expand Down
8 changes: 4 additions & 4 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/lib/pq v1.10.9
gorm.io/driver/mysql v1.5.2-0.20230612053416-48b6526a21f0
gorm.io/driver/postgres v1.5.3-0.20230607070428-18bc84b75196
gorm.io/driver/sqlite v1.5.3
gorm.io/driver/sqlite v1.5.4
gorm.io/driver/sqlserver v1.5.2-0.20230613072041-6e2cde390b0a
gorm.io/gorm v1.25.4
)
Expand All @@ -22,9 +22,9 @@ require (
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
)

replace gorm.io/gorm => ../
42 changes: 42 additions & 0 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,48 @@ func TestMigrateWithSpecialName(t *testing.T) {
AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_2"))
}

// https://github.com/go-gorm/gorm/issues/4760
func TestMigrateAutoIncrement(t *testing.T) {
type AutoIncrementStruct struct {
ID int64 `gorm:"primarykey;autoIncrement"`
Field1 uint32 `gorm:"column:field1"`
Field2 float32 `gorm:"column:field2"`
}

if err := DB.AutoMigrate(&AutoIncrementStruct{}); err != nil {
t.Fatalf("AutoMigrate err: %v", err)
}

const ROWS = 10
for idx := 0; idx < ROWS; idx++ {
if err := DB.Create(&AutoIncrementStruct{}).Error; err != nil {
t.Fatalf("create auto_increment_struct fail, err: %v", err)
}
}

rows := make([]*AutoIncrementStruct, 0, ROWS)
if err := DB.Order("id ASC").Find(&rows).Error; err != nil {
t.Fatalf("find auto_increment_struct fail, err: %v", err)
}

ids := make([]int64, 0, len(rows))
for _, row := range rows {
ids = append(ids, row.ID)
}
lastID := ids[len(ids)-1]

if err := DB.Where("id IN (?)", ids).Delete(&AutoIncrementStruct{}).Error; err != nil {
t.Fatalf("delete auto_increment_struct fail, err: %v", err)
}

newRow := &AutoIncrementStruct{}
if err := DB.Create(newRow).Error; err != nil {
t.Fatalf("create auto_increment_struct fail, err: %v", err)
}

AssertEqual(t, newRow.ID, lastID+1)
}

// https://github.com/go-gorm/gorm/issues/5320
func TestPrimarykeyID(t *testing.T) {
if DB.Dialector.Name() != "postgres" {
Expand Down

0 comments on commit 2095d42

Please sign in to comment.