fix: tables lost when joins exists in from clause, close #5218
commit 7f6a603afa26820e187489b5203f93adc513687c Author: Jinzhu <wosmvp@gmail.com> Date: Sat Apr 2 17:26:48 2022 +0800 Refactor #5218 commit 95d00e6ff2668233f3eca98aa4917291e3d869bd Author: huangcheng1 <huangcheng1@sensetime.com> Date: Fri Apr 1 16:30:27 2022 +0800 fix: tables lost when joins exists in from clause
This commit is contained in:
parent
9144969c83
commit
38a24606da
@ -96,12 +96,12 @@ func BuildQuerySQL(db *gorm.DB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// inline joins
|
// inline joins
|
||||||
joins := []clause.Join{}
|
fromClause := clause.From{}
|
||||||
if fromClause, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok {
|
if v, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok {
|
||||||
joins = fromClause.Joins
|
fromClause = v
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(db.Statement.Joins) != 0 || len(joins) != 0 {
|
if len(db.Statement.Joins) != 0 || len(fromClause.Joins) != 0 {
|
||||||
if len(db.Statement.Selects) == 0 && len(db.Statement.Omits) == 0 && db.Statement.Schema != nil {
|
if len(db.Statement.Selects) == 0 && len(db.Statement.Omits) == 0 && db.Statement.Schema != nil {
|
||||||
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Schema.DBNames))
|
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Schema.DBNames))
|
||||||
for idx, dbName := range db.Statement.Schema.DBNames {
|
for idx, dbName := range db.Statement.Schema.DBNames {
|
||||||
@ -111,7 +111,7 @@ func BuildQuerySQL(db *gorm.DB) {
|
|||||||
|
|
||||||
for _, join := range db.Statement.Joins {
|
for _, join := range db.Statement.Joins {
|
||||||
if db.Statement.Schema == nil {
|
if db.Statement.Schema == nil {
|
||||||
joins = append(joins, clause.Join{
|
fromClause.Joins = append(fromClause.Joins, clause.Join{
|
||||||
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
||||||
})
|
})
|
||||||
} else if relation, ok := db.Statement.Schema.Relationships.Relations[join.Name]; ok {
|
} else if relation, ok := db.Statement.Schema.Relationships.Relations[join.Name]; ok {
|
||||||
@ -176,19 +176,19 @@ func BuildQuerySQL(db *gorm.DB) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
joins = append(joins, clause.Join{
|
fromClause.Joins = append(fromClause.Joins, clause.Join{
|
||||||
Type: clause.LeftJoin,
|
Type: clause.LeftJoin,
|
||||||
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
|
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
|
||||||
ON: clause.Where{Exprs: exprs},
|
ON: clause.Where{Exprs: exprs},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
joins = append(joins, clause.Join{
|
fromClause.Joins = append(fromClause.Joins, clause.Join{
|
||||||
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Statement.AddClause(clause.From{Joins: joins})
|
db.Statement.AddClause(fromClause)
|
||||||
db.Statement.Joins = nil
|
db.Statement.Joins = nil
|
||||||
} else {
|
} else {
|
||||||
db.Statement.AddClauseIfNotExists(clause.From{})
|
db.Statement.AddClauseIfNotExists(clause.From{})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user