forked from mirror/gorm
Allow override alias table in preload conditions
This commit is contained in:
parent
0f3201e73b
commit
3195ae1207
|
@ -50,7 +50,7 @@ func preload(db *gorm.DB, rels []*schema.Relationship, conds []interface{}) {
|
|||
|
||||
joinResults := rel.JoinTable.MakeSlice().Elem()
|
||||
column, values := schema.ToQueryValues(rel.JoinTable.Table, joinForeignKeys, joinForeignValues)
|
||||
tx.Where(clause.IN{Column: column, Values: values}).Find(joinResults.Addr().Interface())
|
||||
db.AddError(tx.Where(clause.IN{Column: column, Values: values}).Find(joinResults.Addr().Interface()).Error)
|
||||
|
||||
// convert join identity map to relation identity map
|
||||
fieldValues := make([]interface{}, len(joinForeignFields))
|
||||
|
@ -93,7 +93,7 @@ func preload(db *gorm.DB, rels []*schema.Relationship, conds []interface{}) {
|
|||
}
|
||||
|
||||
reflectResults := rel.FieldSchema.MakeSlice().Elem()
|
||||
column, values := schema.ToQueryValues(rel.FieldSchema.Table, relForeignKeys, foreignValues)
|
||||
column, values := schema.ToQueryValues(clause.CurrentTable, relForeignKeys, foreignValues)
|
||||
|
||||
for _, cond := range conds {
|
||||
if fc, ok := cond.(func(*gorm.DB) *gorm.DB); ok {
|
||||
|
@ -103,7 +103,7 @@ func preload(db *gorm.DB, rels []*schema.Relationship, conds []interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
tx.Where(clause.IN{Column: column, Values: values}).Find(reflectResults.Addr().Interface(), inlineConds...)
|
||||
db.AddError(tx.Where(clause.IN{Column: column, Values: values}).Find(reflectResults.Addr().Interface(), inlineConds...).Error)
|
||||
|
||||
fieldValues := make([]interface{}, len(relForeignFields))
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"strconv"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
. "gorm.io/gorm/utils/tests"
|
||||
)
|
||||
|
@ -108,6 +109,20 @@ func TestPreloadWithConds(t *testing.T) {
|
|||
}
|
||||
|
||||
CheckUser(t, users2[0], users[0])
|
||||
|
||||
var users3 []User
|
||||
if err := DB.Preload("Account", func(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Table("accounts AS a").Select("a.*")
|
||||
}).Find(&users3, "id IN ?", userIDs).Error; err != nil {
|
||||
t.Errorf("failed to query, got error %v", err)
|
||||
}
|
||||
sort.Slice(users3, func(i, j int) bool {
|
||||
return users2[i].ID < users2[j].ID
|
||||
})
|
||||
|
||||
for i, u := range users3 {
|
||||
CheckUser(t, u, users[i])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNestedPreloadWithConds(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue