-
-
Notifications
You must be signed in to change notification settings - Fork 145
Closed
Description
GORM Playground Link
Sorry, It's such an obvious bug that I don't think there's any need for example code.
Description
The code below does not take some cases into account. I think this is a bug. There is a default value in the existing column property, but if the column property to be changed does not have a default value, no processing is performed. I think if field.HasDefaultValue
is false
, you need to perform a DROP DEFAULT
query.
Lines 383 to 401 in 0d37bf2
if v, ok := fieldColumnType.DefaultValue(); (field.DefaultValueInterface == nil && ok) || v != field.DefaultValue { | |
if field.HasDefaultValue && (field.DefaultValueInterface != nil || field.DefaultValue != "") { | |
if field.DefaultValueInterface != nil { | |
defaultStmt := &gorm.Statement{Vars: []interface{}{field.DefaultValueInterface}} | |
m.Dialector.BindVarTo(defaultStmt, defaultStmt, field.DefaultValueInterface) | |
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? SET DEFAULT ?", m.CurrentTable(stmt), clause.Column{Name: field.DBName}, clause.Expr{SQL: m.Dialector.Explain(defaultStmt.SQL.String(), field.DefaultValueInterface)}).Error; err != nil { | |
return err | |
} | |
} else if field.DefaultValue != "(-)" { | |
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? SET DEFAULT ?", m.CurrentTable(stmt), clause.Column{Name: field.DBName}, clause.Expr{SQL: field.DefaultValue}).Error; err != nil { | |
return err | |
} | |
} else { | |
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? DROP DEFAULT", m.CurrentTable(stmt), clause.Column{Name: field.DBName}, clause.Expr{SQL: field.DefaultValue}).Error; err != nil { | |
return err | |
} | |
} | |
} | |
} |
Metadata
Metadata
Assignees
Labels
No labels