fix: tables lost when joins exists in from clause

This commit is contained in:
huangcheng1 2022-04-01 16:30:27 +08:00
parent f7b52bb649
commit 95d00e6ff2
1 changed files with 3 additions and 1 deletions

View File

@ -96,9 +96,11 @@ func BuildQuerySQL(db *gorm.DB) {
} }
// inline joins // inline joins
tables := []clause.Table{}
joins := []clause.Join{} joins := []clause.Join{}
if fromClause, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok { if fromClause, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok {
joins = fromClause.Joins joins = fromClause.Joins
tables = fromClause.Tables
} }
if len(db.Statement.Joins) != 0 || len(joins) != 0 { if len(db.Statement.Joins) != 0 || len(joins) != 0 {
@ -188,7 +190,7 @@ func BuildQuerySQL(db *gorm.DB) {
} }
} }
db.Statement.AddClause(clause.From{Joins: joins}) db.Statement.AddClause(clause.From{Tables: tables, Joins: joins})
db.Statement.Joins = nil db.Statement.Joins = nil
} else { } else {
db.Statement.AddClauseIfNotExists(clause.From{}) db.Statement.AddClauseIfNotExists(clause.From{})