Fix postgres tests

This commit is contained in:
Jinzhu 2020-05-30 00:16:33 +08:00
parent 6f4602af11
commit db428f221f
1 changed files with 14 additions and 10 deletions

View File

@ -51,29 +51,33 @@ func Query(db *gorm.DB) {
for name, conds := range db.Statement.Joins { for name, conds := range db.Statement.Joins {
if relation, ok := db.Statement.Schema.Relationships.Relations[name]; ok { if relation, ok := db.Statement.Schema.Relationships.Relations[name]; ok {
tableAliasName := relation.Name
for _, s := range relation.FieldSchema.DBNames { for _, s := range relation.FieldSchema.DBNames {
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{ clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
Table: relation.Name, Table: tableAliasName,
Name: s, Name: s,
Alias: relation.Name + "__" + s, Alias: tableAliasName + "__" + s,
}) })
} }
var exprs []clause.Expression var exprs []clause.Expression
for _, ref := range relation.References { for _, ref := range relation.References {
if ref.OwnPrimaryKey { if ref.OwnPrimaryKey {
exprs = append(exprs, clause.Expr{ exprs = append(exprs, clause.Eq{
SQL: fmt.Sprintf("%s.%s = %s.%s", db.Statement.Schema.Table, ref.PrimaryKey.DBName, relation.Name, ref.ForeignKey.DBName), Column: clause.Column{Table: db.Statement.Schema.Table, Name: ref.PrimaryKey.DBName},
Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
}) })
} else { } else {
if ref.PrimaryValue == "" { if ref.PrimaryValue == "" {
exprs = append(exprs, clause.Expr{ exprs = append(exprs, clause.Eq{
SQL: fmt.Sprintf("%s.%s = %s.%s", db.Statement.Schema.Table, ref.ForeignKey.DBName, relation.Name, ref.PrimaryKey.DBName), Column: clause.Column{Table: db.Statement.Schema.Table, Name: ref.ForeignKey.DBName},
Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName},
}) })
} else { } else {
exprs = append(exprs, clause.Expr{ exprs = append(exprs, clause.Eq{
SQL: fmt.Sprintf("%s.%s = ?", relation.Name, ref.PrimaryKey.DBName), Column: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
Vars: []interface{}{ref.PrimaryValue}, Value: ref.PrimaryValue,
}) })
} }
} }
@ -81,7 +85,7 @@ func Query(db *gorm.DB) {
joins = append(joins, clause.Join{ joins = append(joins, clause.Join{
Type: clause.LeftJoin, 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}, ON: clause.Where{Exprs: exprs},
}) })
} else { } else {