Skip to content

Commit

Permalink
Fix auto migration always alert table, close go-gorm#4198
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Nov 29, 2021
1 parent d8a710c commit 3a3b822
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
alterColumn := false

// check size
if length, _ := columnType.Length(); length != int64(field.Size) {
if length, ok := columnType.Length(); length != int64(field.Size) {
if length > 0 && field.Size > 0 {
alterColumn = true
} else {
Expand All @@ -399,7 +399,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
matches := regRealDataType.FindAllStringSubmatch(realDataType, -1)
matches2 := regFullDataType.FindAllStringSubmatch(fullDataType, -1)
if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size) || !field.PrimaryKey) &&
(len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length)) {
(len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length) && ok) {
alterColumn = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
}

func TestSmartMigrateColumn(t *testing.T) {
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
fullSupported := map[string]bool{"mysql": true}[DB.Dialector.Name()]

type UserMigrateColumn struct {
ID uint
Expand Down

0 comments on commit 3a3b822

Please sign in to comment.