Fix invalid preload SQL when no data found, close #4443

This commit is contained in:
Jinzhu 2021-07-13 20:23:05 +08:00
parent c73fe96cfd
commit b13732c450
1 changed files with 9 additions and 7 deletions

View File

@ -104,6 +104,7 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
reflectResults := rel.FieldSchema.MakeSlice().Elem()
column, values := schema.ToQueryValues(clause.CurrentTable, relForeignKeys, foreignValues)
if len(values) != 0 {
for _, cond := range conds {
if fc, ok := cond.(func(*gorm.DB) *gorm.DB); ok {
tx = fc(tx)
@ -113,6 +114,7 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
}
db.AddError(tx.Where(clause.IN{Column: column, Values: values}).Find(reflectResults.Addr().Interface(), inlineConds...).Error)
}
fieldValues := make([]interface{}, len(relForeignFields))