diff --git a/migrator.go b/migrator.go index f39dd9fd..7dddcabf 100644 --- a/migrator.go +++ b/migrator.go @@ -7,16 +7,18 @@ import ( // Migrator returns migrator func (db *DB) Migrator() Migrator { + tx := db.getInstance() + // apply scopes to migrator - for len(db.Statement.scopes) > 0 { - scopes := db.Statement.scopes - db.Statement.scopes = nil + for len(tx.Statement.scopes) > 0 { + scopes := tx.Statement.scopes + tx.Statement.scopes = nil for _, scope := range scopes { - db = scope(db) + tx = scope(tx) } } - return db.Dialector.Migrator(db.Session(&Session{})) + return tx.Dialector.Migrator(tx.Session(&Session{})) } // AutoMigrate run auto migration for given models diff --git a/tests/scopes_test.go b/tests/scopes_test.go index c9787d36..9836c41e 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -45,4 +45,13 @@ func TestScopes(t *testing.T) { if len(users3) != 2 { t.Errorf("Should found two users's name in 1, 3, but got %v", len(users3)) } + + db := DB.Scopes(func(tx *gorm.DB) *gorm.DB { + return tx.Table("custom_table") + }).Session(&gorm.Session{}) + + db.AutoMigrate(&User{}) + if db.Find(&User{}).Statement.Table != "custom_table" { + t.Errorf("failed to call Scopes") + } }