File tree 2 files changed +50
-0
lines changed
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) {
104
104
}
105
105
106
106
joins := []clause.Join {}
107
+
108
+ if fromClause , ok := db .Statement .Clauses ["FROM" ].Expression .(clause.From ); ok {
109
+ joins = fromClause .Joins
110
+ }
111
+
107
112
for _ , join := range db .Statement .Joins {
108
113
if db .Statement .Schema == nil {
109
114
joins = append (joins , clause.Join {
Original file line number Diff line number Diff line change 6
6
"testing"
7
7
8
8
"gorm.io/gorm"
9
+ "gorm.io/gorm/clause"
9
10
. "gorm.io/gorm/utils/tests"
10
11
)
11
12
@@ -242,3 +243,47 @@ func TestCombineStringConditions(t *testing.T) {
242
243
t .Fatalf ("invalid sql generated, got %v" , sql )
243
244
}
244
245
}
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