From 3a3b82263a2e6a3d19c2d669ce9d299b76c47f65 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 29 Nov 2021 20:24:04 +0800 Subject: [PATCH] Fix auto migration always alert table, close #4198 --- migrator/migrator.go | 4 ++-- tests/migrate_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index af1385e2..91bf60a7 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -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 { @@ -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 } } diff --git a/tests/migrate_test.go b/tests/migrate_test.go index 789a5e45..3d15bf2c 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -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