diff --git a/callbacks.go b/callbacks.go index 0da4ecf9..ca6b6d50 100644 --- a/callbacks.go +++ b/callbacks.go @@ -74,7 +74,9 @@ func (cs *callbacks) Raw() *processor { func (p *processor) Execute(db *DB) *DB { // call scopes - db = db.executeScopes(false) + for len(db.Statement.scopes) > 0 { + db = db.executeScopes() + } var ( curTime = time.Now() diff --git a/chainable_api.go b/chainable_api.go index b5099284..19d405cc 100644 --- a/chainable_api.go +++ b/chainable_api.go @@ -366,7 +366,7 @@ func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) { return tx } -func (db *DB) executeScopes(keepScopes bool) (tx *DB) { +func (db *DB) executeScopes() (tx *DB) { tx = db.getInstance() scopes := db.Statement.scopes if len(scopes) == 0 { @@ -393,9 +393,6 @@ func (db *DB) executeScopes(keepScopes bool) (tx *DB) { for _, condition := range conditions { tx.Statement.AddClause(condition) } - if keepScopes { - tx.Statement.scopes = scopes - } return tx } diff --git a/migrator.go b/migrator.go index 6da231b2..037afc35 100644 --- a/migrator.go +++ b/migrator.go @@ -12,7 +12,9 @@ func (db *DB) Migrator() Migrator { tx := db.getInstance() // apply scopes to migrator - tx.executeScopes(false) + for len(tx.Statement.scopes) > 0 { + tx = tx.executeScopes() + } return tx.Dialector.Migrator(tx.Session(&Session{})) } diff --git a/statement.go b/statement.go index 162f0697..59c0b772 100644 --- a/statement.go +++ b/statement.go @@ -324,7 +324,7 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) [] case clause.Expression: conds = append(conds, v) case *DB: - v.executeScopes(true) + v.executeScopes() if cs, ok := v.Statement.Clauses["WHERE"]; ok && cs.Expression != nil { if where, ok := cs.Expression.(clause.Where); ok {