From e85b73e5a5d9de181c12ce4d4ed14da79119cf8a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 19 Mar 2021 13:44:25 +0800 Subject: [PATCH] Fix nested Scopes, close #4196 --- callbacks.go | 10 ++++++---- migrator.go | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/callbacks.go b/callbacks.go index 3e6723a1..315eea17 100644 --- a/callbacks.go +++ b/callbacks.go @@ -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 { diff --git a/migrator.go b/migrator.go index 40936ef9..f39dd9fd 100644 --- a/migrator.go +++ b/migrator.go @@ -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{}))