Skip to content

Commit b0d70a2

Browse files
authored
[#6372] Fixed nullable constraint bug for columns during auto migration (#7269)
* [#6372] Fixed nullable constraint bug for columns during auto migration * [#6372] fix comment * [#6372] Add test code * [#6372] Add test code * [#6372] Fix failed test case * [#6372] Fix failed test case * [#6372] wip * [#6372] wip * [#6372] wip * [#6372] wip
1 parent deceebf commit b0d70a2

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

migrator/migrator.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
524524

525525
// check nullable
526526
if nullable, ok := columnType.Nullable(); ok && nullable == field.NotNull {
527-
// not primary key & database is nullable
528-
if !field.PrimaryKey && nullable {
527+
// not primary key & current database is non-nullable(to be nullable)
528+
if !field.PrimaryKey && !nullable {
529529
alterColumn = true
530530
}
531531
}

tests/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
gorm.io/driver/mysql v1.5.7
1111
gorm.io/driver/postgres v1.5.9
1212
gorm.io/driver/sqlite v1.5.6
13-
gorm.io/driver/sqlserver v1.5.3
13+
gorm.io/driver/sqlserver v1.5.4
1414
gorm.io/gorm v1.25.12
1515
)
1616

tests/migrate_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,48 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
141141
}
142142
}
143143

144+
func TestAutoMigrateNullable(t *testing.T) {
145+
type MigrateNullableColumn struct {
146+
ID uint
147+
Bonus float64 `gorm:"not null"`
148+
Stock float64
149+
}
150+
151+
DB.Migrator().DropTable(&MigrateNullableColumn{})
152+
153+
DB.AutoMigrate(&MigrateNullableColumn{})
154+
155+
type MigrateNullableColumn2 struct {
156+
ID uint
157+
Bonus float64
158+
Stock float64 `gorm:"not null"`
159+
}
160+
161+
if err := DB.Table("migrate_nullable_columns").AutoMigrate(&MigrateNullableColumn2{}); err != nil {
162+
t.Fatalf("failed to auto migrate, got error: %v", err)
163+
}
164+
165+
columnTypes, err := DB.Table("migrate_nullable_columns").Migrator().ColumnTypes(&MigrateNullableColumn{})
166+
if err != nil {
167+
t.Fatalf("failed to get column types, got error: %v", err)
168+
}
169+
170+
for _, columnType := range columnTypes {
171+
switch columnType.Name() {
172+
case "bonus":
173+
// allow to change non-nullable to nullable
174+
if nullable, _ := columnType.Nullable(); !nullable {
175+
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
176+
}
177+
case "stock":
178+
// do not allow to change nullable to non-nullable
179+
if nullable, _ := columnType.Nullable(); !nullable {
180+
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
181+
}
182+
}
183+
}
184+
}
185+
144186
func TestSmartMigrateColumn(t *testing.T) {
145187
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
146188

0 commit comments

Comments
 (0)