fix execute scopes

This commit is contained in:
black 2023-03-23 13:00:26 +08:00
parent ec0b0d6fab
commit 349957c54c
4 changed files with 8 additions and 7 deletions

View File

@ -74,7 +74,9 @@ func (cs *callbacks) Raw() *processor {
func (p *processor) Execute(db *DB) *DB { func (p *processor) Execute(db *DB) *DB {
// call scopes // call scopes
db = db.executeScopes(false) for len(db.Statement.scopes) > 0 {
db = db.executeScopes()
}
var ( var (
curTime = time.Now() curTime = time.Now()

View File

@ -366,7 +366,7 @@ func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) {
return tx return tx
} }
func (db *DB) executeScopes(keepScopes bool) (tx *DB) { func (db *DB) executeScopes() (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
scopes := db.Statement.scopes scopes := db.Statement.scopes
if len(scopes) == 0 { if len(scopes) == 0 {
@ -393,9 +393,6 @@ func (db *DB) executeScopes(keepScopes bool) (tx *DB) {
for _, condition := range conditions { for _, condition := range conditions {
tx.Statement.AddClause(condition) tx.Statement.AddClause(condition)
} }
if keepScopes {
tx.Statement.scopes = scopes
}
return tx return tx
} }

View File

@ -12,7 +12,9 @@ func (db *DB) Migrator() Migrator {
tx := db.getInstance() tx := db.getInstance()
// apply scopes to migrator // apply scopes to migrator
tx.executeScopes(false) for len(tx.Statement.scopes) > 0 {
tx = tx.executeScopes()
}
return tx.Dialector.Migrator(tx.Session(&Session{})) return tx.Dialector.Migrator(tx.Session(&Session{}))
} }

View File

@ -324,7 +324,7 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
case clause.Expression: case clause.Expression:
conds = append(conds, v) conds = append(conds, v)
case *DB: case *DB:
v.executeScopes(true) v.executeScopes()
if cs, ok := v.Statement.Clauses["WHERE"]; ok && cs.Expression != nil { if cs, ok := v.Statement.Clauses["WHERE"]; ok && cs.Expression != nil {
if where, ok := cs.Expression.(clause.Where); ok { if where, ok := cs.Expression.(clause.Where); ok {