diff --git a/migrator.go b/migrator.go index 3dec4a7..35b291b 100644 --- a/migrator.go +++ b/migrator.go @@ -315,7 +315,7 @@ func (m Migrator) AlterColumn(value interface{}, field string) error { return err } } else { - if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ? USING ?::?", + if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ?"+m.genUsingExpression(fileType.SQL, fieldColumnType.DatabaseTypeName()), m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType, clause.Column{Name: field.DBName}, fileType).Error; err != nil { return err } @@ -375,6 +375,16 @@ func (m Migrator) AlterColumn(value interface{}, field string) error { return nil } +func (m Migrator) genUsingExpression(targetType, sourceType string) string { + if targetType == "boolean" { + switch sourceType { + case "int2", "int8", "numeric": + return " USING ?::INT::?" + } + } + return " USING ?::?" +} + func (m Migrator) HasConstraint(value interface{}, name string) bool { var count int64 m.RunWithValue(value, func(stmt *gorm.Statement) error {