Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sqlite dialector cannot apply PRIMARY KEY AUTOINCREMENT type #6624

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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