Fix nested Scopes, close #4196

This commit is contained in:
Jinzhu 2021-03-19 13:44:25 +08:00
parent a3d9bbfc36
commit e85b73e5a5
2 changed files with 12 additions and 8 deletions

View File

@ -109,10 +109,12 @@ func (p *processor) Execute(db *DB) {
}
// call scopes
scopes := stmt.scopes
stmt.scopes = nil
for _, scope := range scopes {
db = scope(db)
for len(stmt.scopes) > 0 {
scopes := stmt.scopes
stmt.scopes = nil
for _, scope := range scopes {
db = scope(db)
}
}
for _, f := range p.fns {

View File

@ -8,10 +8,12 @@ import (
// Migrator returns migrator
func (db *DB) Migrator() Migrator {
// apply scopes to migrator
scopes := db.Statement.scopes
db.Statement.scopes = nil
for _, scope := range scopes {
db = scope(db)
for len(db.Statement.scopes) > 0 {
scopes := db.Statement.scopes
db.Statement.scopes = nil
for _, scope := range scopes {
db = scope(db)
}
}
return db.Dialector.Migrator(db.Session(&Session{}))