Use specified table when preloading data with Join

This commit is contained in:
Jinzhu 2020-07-31 14:47:26 +08:00
parent 81c68db87f
commit dc299b900f
1 changed files with 2 additions and 2 deletions

View File

@ -124,13 +124,13 @@ func BuildQuerySQL(db *gorm.DB) {
for idx, ref := range relation.References { for idx, ref := range relation.References {
if ref.OwnPrimaryKey { if ref.OwnPrimaryKey {
exprs[idx] = clause.Eq{ exprs[idx] = clause.Eq{
Column: clause.Column{Table: db.Statement.Schema.Table, Name: ref.PrimaryKey.DBName}, Column: clause.Column{Table: clause.CurrentTable, Name: ref.PrimaryKey.DBName},
Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName}, Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
} }
} else { } else {
if ref.PrimaryValue == "" { if ref.PrimaryValue == "" {
exprs[idx] = clause.Eq{ exprs[idx] = clause.Eq{
Column: clause.Column{Table: db.Statement.Schema.Table, Name: ref.ForeignKey.DBName}, Column: clause.Column{Table: clause.CurrentTable, Name: ref.ForeignKey.DBName},
Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName}, Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName},
} }
} else { } else {