From db428f221f8a09c1af532fb248ffffd18082a156 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 30 May 2020 00:16:33 +0800 Subject: [PATCH] Fix postgres tests --- callbacks/query.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/callbacks/query.go b/callbacks/query.go index ee3f5c8d..6edfee0b 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -51,29 +51,33 @@ func Query(db *gorm.DB) { for name, conds := range db.Statement.Joins { if relation, ok := db.Statement.Schema.Relationships.Relations[name]; ok { + tableAliasName := relation.Name + for _, s := range relation.FieldSchema.DBNames { clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{ - Table: relation.Name, + Table: tableAliasName, Name: s, - Alias: relation.Name + "__" + s, + Alias: tableAliasName + "__" + s, }) } var exprs []clause.Expression for _, ref := range relation.References { if ref.OwnPrimaryKey { - exprs = append(exprs, clause.Expr{ - SQL: fmt.Sprintf("%s.%s = %s.%s", db.Statement.Schema.Table, ref.PrimaryKey.DBName, relation.Name, ref.ForeignKey.DBName), + exprs = append(exprs, clause.Eq{ + Column: clause.Column{Table: db.Statement.Schema.Table, Name: ref.PrimaryKey.DBName}, + Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName}, }) } else { if ref.PrimaryValue == "" { - exprs = append(exprs, clause.Expr{ - SQL: fmt.Sprintf("%s.%s = %s.%s", db.Statement.Schema.Table, ref.ForeignKey.DBName, relation.Name, ref.PrimaryKey.DBName), + exprs = append(exprs, clause.Eq{ + Column: clause.Column{Table: db.Statement.Schema.Table, Name: ref.ForeignKey.DBName}, + Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName}, }) } else { - exprs = append(exprs, clause.Expr{ - SQL: fmt.Sprintf("%s.%s = ?", relation.Name, ref.PrimaryKey.DBName), - Vars: []interface{}{ref.PrimaryValue}, + exprs = append(exprs, clause.Eq{ + Column: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName}, + Value: ref.PrimaryValue, }) } } @@ -81,7 +85,7 @@ func Query(db *gorm.DB) { joins = append(joins, clause.Join{ Type: clause.LeftJoin, - Table: clause.Table{Name: relation.FieldSchema.Table, Alias: relation.Name}, + Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName}, ON: clause.Where{Exprs: exprs}, }) } else {