File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,11 @@ func BuildQuerySQL(db *gorm.DB) {
104104 }
105105
106106 joins := []clause.Join {}
107+
108+ if fromClause , ok := db .Statement .Clauses ["FROM" ].Expression .(clause.From ); ok {
109+ joins = fromClause .Joins
110+ }
111+
107112 for _ , join := range db .Statement .Joins {
108113 if db .Statement .Schema == nil {
109114 joins = append (joins , clause.Join {
Original file line number Diff line number Diff line change 66 "testing"
77
88 "gorm.io/gorm"
9+ "gorm.io/gorm/clause"
910 . "gorm.io/gorm/utils/tests"
1011)
1112
@@ -242,3 +243,47 @@ func TestCombineStringConditions(t *testing.T) {
242243 t .Fatalf ("invalid sql generated, got %v" , sql )
243244 }
244245}
246+
247+ func TestFromWithJoins (t * testing.T ) {
248+ var result User
249+
250+ newDB := DB .Session (& gorm.Session {NewDB : true , DryRun : true }).Table ("users" )
251+
252+ newDB .Clauses (
253+ clause.From {
254+ Tables : []clause.Table {{Name : "users" }},
255+ Joins : []clause.Join {
256+ {
257+ Table : clause.Table {Name : "companies" , Raw : false },
258+ ON : clause.Where {
259+ Exprs : []clause.Expression {
260+ clause.Eq {
261+ Column : clause.Column {
262+ Table : "users" ,
263+ Name : "company_id" ,
264+ },
265+ Value : clause.Column {
266+ Table : "companies" ,
267+ Name : "id" ,
268+ },
269+ },
270+ },
271+ },
272+ },
273+ },
274+ },
275+ )
276+
277+ newDB .Joins ("inner join rgs on rgs.id = user.id" )
278+
279+ stmt := newDB .First (& result ).Statement
280+ str := stmt .SQL .String ()
281+
282+ if ! strings .Contains (str , "rgs.id = user.id" ) {
283+ t .Errorf ("The second join condition is over written instead of combining" )
284+ }
285+
286+ if ! strings .Contains (str , "`users`.`company_id` = `companies`.`id`" ) && ! strings .Contains (str , "\" users\" .\" company_id\" = \" companies\" .\" id\" " ) {
287+ t .Errorf ("The first join condition is over written instead of combining" )
288+ }
289+ }
You can’t perform that action at this time.
0 commit comments