@@ -111,14 +111,16 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
111
111
func (m Migrator ) AlterColumn (value interface {}, field string ) error {
112
112
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
113
113
if field := stmt .Schema .LookUpField (field ); field != nil {
114
- fileType := clause.Expr {SQL : m .DataTypeOf (field )}
114
+ fieldType := clause.Expr {SQL : m .DataTypeOf (field )}
115
115
if field .NotNull {
116
- fileType .SQL += " NOT NULL"
116
+ fieldType .SQL += " NOT NULL"
117
+ } else {
118
+ fieldType .SQL += " NULL"
117
119
}
118
120
119
121
return m .DB .Exec (
120
122
"ALTER TABLE ? ALTER COLUMN ? ?" ,
121
- clause.Table {Name : stmt .Table }, clause.Column {Name : field .DBName }, fileType ,
123
+ clause.Table {Name : stmt .Table }, clause.Column {Name : field .DBName }, fieldType ,
122
124
).Error
123
125
}
124
126
return fmt .Errorf ("failed to look up field with name: %s" , field )
@@ -171,7 +173,10 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
171
173
172
174
for columns .Next () {
173
175
var (
174
- column migrator.ColumnType
176
+ column = migrator.ColumnType {
177
+ PrimaryKeyValue : sql.NullBool {Valid : true },
178
+ UniqueValue : sql.NullBool {Valid : true },
179
+ }
175
180
datetimePrecision sql.NullInt64
176
181
radixValue sql.NullInt64
177
182
nullableValue sql.NullString
@@ -198,6 +203,8 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
198
203
column .DefaultValueValue .String = matches [1 ]
199
204
matches = defaultValueTrimRegexp .FindStringSubmatch (column .DefaultValueValue .String )
200
205
}
206
+ } else {
207
+ column .DefaultValueValue .Valid = true
201
208
}
202
209
203
210
for _ , c := range rawColumnTypes {
@@ -224,7 +231,7 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
224
231
if mc .NameValue .String == name {
225
232
switch columnType {
226
233
case "PRIMARY KEY" :
227
- mc .PrimayKeyValue = sql.NullBool {Bool : true , Valid : true }
234
+ mc .PrimaryKeyValue = sql.NullBool {Bool : true , Valid : true }
228
235
case "UNIQUE" :
229
236
mc .UniqueValue = sql.NullBool {Bool : true , Valid : true }
230
237
}
0 commit comments