go-gorm / playground

GORM Playground (Please Create PR for this project to report issues)
MIT License
89 stars 681 forks source link

Added example JOIN scope. #743

Open systemmonkey42 opened 3 months ago

systemmonkey42 commented 3 months ago

Explain your user case and expected results

Test case for issue go-gorm/gorm#7076

Gorm "Scopes" don't work as advertised when used as a join condition.

In this particular case, the following diff naively resolves the issue

diff --git chainable_api.go chainable_api.go
index e6c90cefe268..84c77c958634 100644
--- chainable_api.go
+++ chainable_api.go
@@ -257,6 +257,7 @@ func joins(db *DB, joinType clause.JoinType, query string, args ...interface{})
                Name: query, Conds: args, Selects: db.Statement.Selects,
                Omits: db.Statement.Omits, JoinType: joinType,
            }
+           db = db.executeScopes()
            if where, ok := db.Statement.Clauses["WHERE"].Expression.(clause.Where); ok {
                j.On = &where
            }