Skip to content

Commit 9b64c96

Browse files
jinzhumittwillson
authored andcommitted
Revert "Don't override the from clauses, close go-gorm#4129" close go-gorm#4139
This reverts commit 6647552.
1 parent 71cdf8a commit 9b64c96

File tree

2 files changed

+0
-50
lines changed

2 files changed

+0
-50
lines changed

callbacks/query.go

-5
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ 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-
112107
for _, join := range db.Statement.Joins {
113108
if db.Statement.Schema == nil {
114109
joins = append(joins, clause.Join{

tests/sql_builder_test.go

-45
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
"gorm.io/gorm"
9-
"gorm.io/gorm/clause"
109
. "gorm.io/gorm/utils/tests"
1110
)
1211

@@ -243,47 +242,3 @@ func TestCombineStringConditions(t *testing.T) {
243242
t.Fatalf("invalid sql generated, got %v", sql)
244243
}
245244
}
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-
}

0 commit comments

Comments
 (0)