(values ...interface{})
| 197 | } |
| 198 | |
| 199 | func (association *Association) Delete(values ...interface{}) error { |
| 200 | values = expandValues(values) |
| 201 | |
| 202 | if association.Error == nil { |
| 203 | var ( |
| 204 | reflectValue = association.DB.Statement.ReflectValue |
| 205 | rel = association.Relationship |
| 206 | primaryFields []*schema.Field |
| 207 | foreignKeys []string |
| 208 | updateAttrs = map[string]interface{}{} |
| 209 | conds []clause.Expression |
| 210 | ) |
| 211 | |
| 212 | for _, ref := range rel.References { |
| 213 | if ref.PrimaryValue == "" { |
| 214 | primaryFields = append(primaryFields, ref.PrimaryKey) |
| 215 | foreignKeys = append(foreignKeys, ref.ForeignKey.DBName) |
| 216 | updateAttrs[ref.ForeignKey.DBName] = nil |
| 217 | } else { |
| 218 | conds = append(conds, clause.Eq{Column: ref.ForeignKey.DBName, Value: ref.PrimaryValue}) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | switch rel.Type { |
| 223 | case schema.BelongsTo: |
| 224 | associationDB := association.DB.Session(&Session{}) |
| 225 | tx := associationDB.Model(reflect.New(rel.Schema.ModelType).Interface()) |
| 226 | |
| 227 | _, pvs := schema.GetIdentityFieldValuesMap(association.DB.Statement.Context, reflectValue, rel.Schema.PrimaryFields) |
| 228 | if pcolumn, pvalues := schema.ToQueryValues(rel.Schema.Table, rel.Schema.PrimaryFieldDBNames, pvs); len(pvalues) > 0 { |
| 229 | conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues}) |
| 230 | } else { |
| 231 | return ErrPrimaryKeyRequired |
| 232 | } |
| 233 | |
| 234 | _, rvs := schema.GetIdentityFieldValuesMapFromValues(association.DB.Statement.Context, values, primaryFields) |
| 235 | relColumn, relValues := schema.ToQueryValues(rel.Schema.Table, foreignKeys, rvs) |
| 236 | conds = append(conds, clause.IN{Column: relColumn, Values: relValues}) |
| 237 | |
| 238 | association.Error = tx.Clauses(conds...).UpdateColumns(updateAttrs).Error |
| 239 | if association.Unscope { |
| 240 | var foreignFields []*schema.Field |
| 241 | for _, ref := range rel.References { |
| 242 | if !ref.OwnPrimaryKey { |
| 243 | foreignFields = append(foreignFields, ref.ForeignKey) |
| 244 | } |
| 245 | } |
| 246 | if _, fvs := schema.GetIdentityFieldValuesMap(association.DB.Statement.Context, reflectValue, foreignFields); len(fvs) > 0 { |
| 247 | column, values := schema.ToQueryValues(rel.FieldSchema.Table, rel.FieldSchema.PrimaryFieldDBNames, fvs) |
| 248 | association.Error = associationDB.Model(nil).Where(clause.IN{Column: column, Values: values}).Delete(reflect.New(rel.FieldSchema.ModelType).Interface()).Error |
| 249 | } |
| 250 | } |
| 251 | case schema.HasOne, schema.HasMany: |
| 252 | model := reflect.New(rel.FieldSchema.ModelType).Interface() |
| 253 | tx := association.DB.Model(model) |
| 254 | |
| 255 | _, pvs := schema.GetIdentityFieldValuesMap(association.DB.Statement.Context, reflectValue, primaryFields) |
| 256 | if pcolumn, pvalues := schema.ToQueryValues(rel.FieldSchema.Table, foreignKeys, pvs); len(pvalues) > 0 { |
nothing calls this directly
no test coverage detected