From e541ca5cdf1b24eb2b18e6662d22ffa286391a58 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 16 Dec 2015 21:00:56 +0800 Subject: [PATCH] Fix DropTableIfExists with string --- main.go | 4 ++++ preload_test.go | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 26fc3f18..8e5ed1ac 100644 --- a/main.go +++ b/main.go @@ -392,6 +392,10 @@ func (s *DB) DropTable(values ...interface{}) *DB { func (s *DB) DropTableIfExists(values ...interface{}) *DB { db := s.clone() for _, value := range values { + if tableName, ok := value.(string); ok { + db = db.Table(tableName) + } + db = db.NewScope(value).dropTableIfExists().db } return db diff --git a/preload_test.go b/preload_test.go index b5188628..48d5db02 100644 --- a/preload_test.go +++ b/preload_test.go @@ -632,7 +632,7 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) { DB.DropTableIfExists(&Level2{}) DB.DropTableIfExists(&Level1{}) - DB.Table("levels").DropTableIfExists("levels") + DB.DropTableIfExists("levels") if err := DB.AutoMigrate(&Level2{}, &Level1{}).Error; err != nil { panic(err) @@ -717,7 +717,7 @@ func TestManyToManyPreloadForPointer(t *testing.T) { DB.DropTableIfExists(&Level2{}) DB.DropTableIfExists(&Level1{}) - DB.Table("levels").DropTableIfExists("levels") + DB.DropTableIfExists("levels") if err := DB.AutoMigrate(&Level2{}, &Level1{}).Error; err != nil { panic(err) @@ -808,7 +808,7 @@ func TestManyToManyPreloadForNestedPointer(t *testing.T) { DB.DropTableIfExists(&Level3{}) DB.DropTableIfExists(&Level2{}) DB.DropTableIfExists(&Level1{}) - DB.Table("levels").DropTableIfExists("levels") + DB.DropTableIfExists("levels") if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil { panic(err)