@@ -141,6 +141,48 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
141
141
}
142
142
}
143
143
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
+
144
186
func TestSmartMigrateColumn (t * testing.T ) {
145
187
fullSupported := map [string ]bool {"mysql" : true , "postgres" : true }[DB .Dialector .Name ()]
146
188
0 commit comments