Skip to content

Commit

Permalink
fix: AutoMigrate panic
Browse files Browse the repository at this point in the history
  • Loading branch information
a631807682 committed Apr 7, 2022
1 parent 55755e3 commit 28f957e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
if field := stmt.Schema.LookUpField(field); field != nil {
var (
columnTypes, _ = m.DB.Migrator().ColumnTypes(value)
fieldColumnType migrator.ColumnType
fieldColumnType *migrator.ColumnType
)
for _, columnType := range columnTypes {
if columnType.Name() == field.DBName {
fieldColumnType, _ = columnType.(migrator.ColumnType)
fieldColumnType, _ = columnType.(*migrator.ColumnType)
}
}

Expand Down Expand Up @@ -384,10 +384,11 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
if err != nil {
return err
}
for _, columnType := range columnTypes {
for i, columnType := range columnTypes {
for _, c := range rawColumnTypes {
if c.Name() == columnType.Name() {
columnType.(*migrator.ColumnType).SQLColumnType = c
mc := columnTypes[i].(*migrator.ColumnType)
mc.SQLColumnType = c
break
}
}
Expand All @@ -405,8 +406,8 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
for columnTypeRows.Next() {
var name, columnType string
columnTypeRows.Scan(&name, &columnType)
for _, c := range columnTypes {
mc := c.(*migrator.ColumnType)
for i := range columnTypes {
mc := columnTypes[i].(*migrator.ColumnType)
if mc.NameValue.String == name {
switch columnType {
case "PRIMARY KEY":
Expand Down Expand Up @@ -435,8 +436,8 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
for dataTypeRows.Next() {
var name, dataType string
dataTypeRows.Scan(&name, &dataType)
for _, c := range columnTypes {
mc := c.(*migrator.ColumnType)
for i := range columnTypes {
mc := columnTypes[i].(*migrator.ColumnType)
if mc.NameValue.String == name {
mc.ColumnTypeValue = sql.NullString{String: dataType, Valid: true}
break
Expand Down

0 comments on commit 28f957e

Please sign in to comment.