adjust code for preload
This commit is contained in:
parent
c170af11e9
commit
e841ee0b7f
@ -26,11 +26,20 @@ func BeforeDelete(db *gorm.DB) {
|
||||
func DeleteBeforeAssociations(db *gorm.DB) {
|
||||
if db.Error == nil && db.Statement.Schema != nil {
|
||||
selectColumns, restricted := db.Statement.SelectAndOmitColumns(true, false)
|
||||
if !restricted {
|
||||
return
|
||||
}
|
||||
|
||||
if restricted {
|
||||
for column, v := range selectColumns {
|
||||
if v {
|
||||
if rel, ok := db.Statement.Schema.Relationships.Relations[column]; ok {
|
||||
if !v {
|
||||
continue
|
||||
}
|
||||
|
||||
rel, ok := db.Statement.Schema.Relationships.Relations[column]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
switch rel.Type {
|
||||
case schema.HasOne, schema.HasMany:
|
||||
queryConds := rel.ToQueryConditions(db.Statement.ReflectValue)
|
||||
@ -46,8 +55,8 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||
for _, s := range db.Statement.Selects {
|
||||
if s == clause.Associations {
|
||||
selects = append(selects, s)
|
||||
} else if strings.HasPrefix(s, column+".") {
|
||||
selects = append(selects, strings.TrimPrefix(s, column+"."))
|
||||
} else if columnPrefix := column + "."; strings.HasPrefix(s, columnPrefix) {
|
||||
selects = append(selects, strings.TrimPrefix(s, columnPrefix))
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,11 +72,9 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||
}
|
||||
}
|
||||
|
||||
if !withoutConditions {
|
||||
if db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
||||
if !withoutConditions && db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
case schema.Many2Many:
|
||||
var (
|
||||
queryConds = make([]clause.Expression, 0, len(rel.References))
|
||||
@ -99,9 +106,7 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,7 +145,13 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||
fieldValues[idx], _ = field.ValueOf(elem)
|
||||
}
|
||||
|
||||
if datas, ok := identityMap[utils.ToStringKey(fieldValues...)]; ok {
|
||||
datas, ok := identityMap[utils.ToStringKey(fieldValues...)]
|
||||
if !ok {
|
||||
db.AddError(fmt.Errorf("failed to assign association %#v, make sure foreign fields exists",
|
||||
elem.Interface()))
|
||||
continue
|
||||
}
|
||||
|
||||
for _, data := range datas {
|
||||
reflectFieldValue := rel.Field.ReflectValueOf(data)
|
||||
if reflectFieldValue.Kind() == reflect.Ptr && reflectFieldValue.IsNil() {
|
||||
@ -155,7 +161,7 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||
reflectFieldValue = reflect.Indirect(reflectFieldValue)
|
||||
switch reflectFieldValue.Kind() {
|
||||
case reflect.Struct:
|
||||
rel.Field.Set(data, reflectResults.Index(i).Interface())
|
||||
rel.Field.Set(data, elem.Interface())
|
||||
case reflect.Slice, reflect.Array:
|
||||
if reflectFieldValue.Type().Elem().Kind() == reflect.Ptr {
|
||||
rel.Field.Set(data, reflect.Append(reflectFieldValue, elem).Interface())
|
||||
@ -164,8 +170,5 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
db.AddError(fmt.Errorf("failed to assign association %#v, make sure foreign fields exists", elem.Interface()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user