@@ -127,6 +127,11 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
127
127
}
128
128
} else {
129
129
if err := m .RunWithValue (value , func (stmt * gorm.Statement ) error {
130
+
131
+ if stmt .Schema == nil {
132
+ return errors .New ("failed to get schema" )
133
+ }
134
+
130
135
columnTypes , err := queryTx .Migrator ().ColumnTypes (value )
131
136
if err != nil {
132
137
return err
@@ -211,6 +216,11 @@ func (m Migrator) CreateTable(values ...interface{}) error {
211
216
for _ , value := range m .ReorderModels (values , false ) {
212
217
tx := m .DB .Session (& gorm.Session {})
213
218
if err := m .RunWithValue (value , func (stmt * gorm.Statement ) (err error ) {
219
+
220
+ if stmt .Schema == nil {
221
+ return errors .New ("failed to get schema" )
222
+ }
223
+
214
224
var (
215
225
createTableSQL = "CREATE TABLE ? ("
216
226
values = []interface {}{m .CurrentTable (stmt )}
@@ -363,6 +373,9 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error {
363
373
func (m Migrator ) AddColumn (value interface {}, name string ) error {
364
374
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
365
375
// avoid using the same name field
376
+ if stmt .Schema == nil {
377
+ return errors .New ("failed to get schema" )
378
+ }
366
379
f := stmt .Schema .LookUpField (name )
367
380
if f == nil {
368
381
return fmt .Errorf ("failed to look up field with name: %s" , name )
@@ -382,8 +395,10 @@ func (m Migrator) AddColumn(value interface{}, name string) error {
382
395
// DropColumn drop value's `name` column
383
396
func (m Migrator ) DropColumn (value interface {}, name string ) error {
384
397
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
385
- if field := stmt .Schema .LookUpField (name ); field != nil {
386
- name = field .DBName
398
+ if stmt .Schema != nil {
399
+ if field := stmt .Schema .LookUpField (name ); field != nil {
400
+ name = field .DBName
401
+ }
387
402
}
388
403
389
404
return m .DB .Exec (
@@ -395,13 +410,15 @@ func (m Migrator) DropColumn(value interface{}, name string) error {
395
410
// AlterColumn alter value's `field` column' type based on schema definition
396
411
func (m Migrator ) AlterColumn (value interface {}, field string ) error {
397
412
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
398
- if field := stmt .Schema .LookUpField (field ); field != nil {
399
- fileType := m .FullDataTypeOf (field )
400
- return m .DB .Exec (
401
- "ALTER TABLE ? ALTER COLUMN ? TYPE ?" ,
402
- m .CurrentTable (stmt ), clause.Column {Name : field .DBName }, fileType ,
403
- ).Error
413
+ if stmt .Schema != nil {
414
+ if field := stmt .Schema .LookUpField (field ); field != nil {
415
+ fileType := m .FullDataTypeOf (field )
416
+ return m .DB .Exec (
417
+ "ALTER TABLE ? ALTER COLUMN ? TYPE ?" ,
418
+ m .CurrentTable (stmt ), clause.Column {Name : field .DBName }, fileType ,
419
+ ).Error
404
420
421
+ }
405
422
}
406
423
return fmt .Errorf ("failed to look up field with name: %s" , field )
407
424
})
@@ -413,8 +430,10 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
413
430
m .RunWithValue (value , func (stmt * gorm.Statement ) error {
414
431
currentDatabase := m .DB .Migrator ().CurrentDatabase ()
415
432
name := field
416
- if field := stmt .Schema .LookUpField (field ); field != nil {
417
- name = field .DBName
433
+ if stmt .Schema != nil {
434
+ if field := stmt .Schema .LookUpField (field ); field != nil {
435
+ name = field .DBName
436
+ }
418
437
}
419
438
420
439
return m .DB .Raw (
@@ -429,12 +448,14 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
429
448
// RenameColumn rename value's field name from oldName to newName
430
449
func (m Migrator ) RenameColumn (value interface {}, oldName , newName string ) error {
431
450
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
432
- if field := stmt .Schema .LookUpField (oldName ); field != nil {
433
- oldName = field .DBName
434
- }
451
+ if stmt .Schema != nil {
452
+ if field := stmt .Schema .LookUpField (oldName ); field != nil {
453
+ oldName = field .DBName
454
+ }
435
455
436
- if field := stmt .Schema .LookUpField (newName ); field != nil {
437
- newName = field .DBName
456
+ if field := stmt .Schema .LookUpField (newName ); field != nil {
457
+ newName = field .DBName
458
+ }
438
459
}
439
460
440
461
return m .DB .Exec (
@@ -794,6 +815,9 @@ type BuildIndexOptionsInterface interface {
794
815
// CreateIndex create index `name`
795
816
func (m Migrator ) CreateIndex (value interface {}, name string ) error {
796
817
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
818
+ if stmt .Schema == nil {
819
+ return errors .New ("failed to get schema" )
820
+ }
797
821
if idx := stmt .Schema .LookIndex (name ); idx != nil {
798
822
opts := m .DB .Migrator ().(BuildIndexOptionsInterface ).BuildIndexOptions (idx .Fields , stmt )
799
823
values := []interface {}{clause.Column {Name : idx .Name }, m .CurrentTable (stmt ), opts }
@@ -826,8 +850,10 @@ func (m Migrator) CreateIndex(value interface{}, name string) error {
826
850
// DropIndex drop index `name`
827
851
func (m Migrator ) DropIndex (value interface {}, name string ) error {
828
852
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
829
- if idx := stmt .Schema .LookIndex (name ); idx != nil {
830
- name = idx .Name
853
+ if stmt .Schema != nil {
854
+ if idx := stmt .Schema .LookIndex (name ); idx != nil {
855
+ name = idx .Name
856
+ }
831
857
}
832
858
833
859
return m .DB .Exec ("DROP INDEX ? ON ?" , clause.Column {Name : name }, m .CurrentTable (stmt )).Error
@@ -839,8 +865,10 @@ func (m Migrator) HasIndex(value interface{}, name string) bool {
839
865
var count int64
840
866
m .RunWithValue (value , func (stmt * gorm.Statement ) error {
841
867
currentDatabase := m .DB .Migrator ().CurrentDatabase ()
842
- if idx := stmt .Schema .LookIndex (name ); idx != nil {
843
- name = idx .Name
868
+ if stmt .Schema != nil {
869
+ if idx := stmt .Schema .LookIndex (name ); idx != nil {
870
+ name = idx .Name
871
+ }
844
872
}
845
873
846
874
return m .DB .Raw (
0 commit comments