Add BelongsTo support for association Delete
This commit is contained in:
		
							parent
							
								
									d57867eb46
								
							
						
					
					
						commit
						c85f00bcd2
					
				@ -83,17 +83,19 @@ func (association *Association) Append(values ...interface{}) *Association {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (association *Association) Delete(values ...interface{}) *Association {
 | 
					func (association *Association) Delete(values ...interface{}) *Association {
 | 
				
			||||||
	scope := association.Scope
 | 
						scope := association.Scope
 | 
				
			||||||
 | 
						query := scope.NewDB()
 | 
				
			||||||
	relationship := association.Field.Relationship
 | 
						relationship := association.Field.Relationship
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// many to many
 | 
						// many to many
 | 
				
			||||||
	if relationship.Kind == "many_to_many" {
 | 
						if relationship.Kind == "many_to_many" {
 | 
				
			||||||
		query := scope.NewDB()
 | 
							// current value's foreign keys
 | 
				
			||||||
		for idx, foreignKey := range relationship.ForeignDBNames {
 | 
							for idx, foreignKey := range relationship.ForeignDBNames {
 | 
				
			||||||
			if field, ok := scope.FieldByName(relationship.ForeignFieldNames[idx]); ok {
 | 
								if field, ok := scope.FieldByName(relationship.ForeignFieldNames[idx]); ok {
 | 
				
			||||||
				query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
 | 
									query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// deleting value's foreign keys
 | 
				
			||||||
		primaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, values...)
 | 
							primaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, values...)
 | 
				
			||||||
		sql := fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(primaryKeys))
 | 
							sql := fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(primaryKeys))
 | 
				
			||||||
		query = query.Where(sql, toQueryValues(primaryKeys)...)
 | 
							query = query.Where(sql, toQueryValues(primaryKeys)...)
 | 
				
			||||||
@ -116,7 +118,19 @@ func (association *Association) Delete(values ...interface{}) *Association {
 | 
				
			|||||||
			association.Field.Set(leftValues)
 | 
								association.Field.Set(leftValues)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		association.setErr(errors.New("delete only support many to many"))
 | 
							association.Field.Set(reflect.Zero(association.Field.Field.Type()))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if relationship.Kind == "belongs_to" {
 | 
				
			||||||
 | 
								var foreignKeyMap = map[string]interface{}{}
 | 
				
			||||||
 | 
								for _, foreignKey := range relationship.ForeignDBNames {
 | 
				
			||||||
 | 
									foreignKeyMap[foreignKey] = nil
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								primaryKeys := association.getPrimaryKeys(relationship.AssociationForeignFieldNames, values...)
 | 
				
			||||||
 | 
								sql := fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys))
 | 
				
			||||||
 | 
								query.Model(scope.Value).Where(sql, toQueryValues(primaryKeys)...).Update(foreignKeyMap)
 | 
				
			||||||
 | 
							} else if relationship.Kind == "has_one" || relationship.Kind == "has_many" {
 | 
				
			||||||
 | 
								// query.Model(association.Field).UpdateColumn(foreignKeyMap)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return association
 | 
						return association
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -150,9 +164,9 @@ func (association *Association) Replace(values ...interface{}) *Association {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	query := scope.NewDB()
 | 
						query := scope.NewDB()
 | 
				
			||||||
	var foreignKeyMap = map[string]string{}
 | 
						var foreignKeyMap = map[string]interface{}{}
 | 
				
			||||||
	for idx, foreignKey := range relationship.ForeignDBNames {
 | 
						for idx, foreignKey := range relationship.ForeignDBNames {
 | 
				
			||||||
		foreignKeyMap[foreignKey] = ""
 | 
							foreignKeyMap[foreignKey] = nil
 | 
				
			||||||
		if field, ok := scope.FieldByName(relationship.ForeignFieldNames[idx]); ok {
 | 
							if field, ok := scope.FieldByName(relationship.ForeignFieldNames[idx]); ok {
 | 
				
			||||||
			query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
 | 
								query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(foreignKey)), field.Field.Interface())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -5,15 +5,14 @@ import (
 | 
				
			|||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestHasOne(t *testing.T) {
 | 
					func TestBelongsTo(t *testing.T) {
 | 
				
			||||||
	DB.DropTable(Category{}, Post{})
 | 
						DB.DropTable(Category{}, Post{})
 | 
				
			||||||
	DB.CreateTable(Category{}, Post{})
 | 
						DB.CreateTable(Category{}, Post{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	post := Post{
 | 
						post := Post{
 | 
				
			||||||
		Title:        "post 1",
 | 
							Title:    "post 1",
 | 
				
			||||||
		Body:         "body 1",
 | 
							Body:     "body 1",
 | 
				
			||||||
		Category:     Category{Name: "Category 1"},
 | 
							Category: Category{Name: "Category 1"},
 | 
				
			||||||
		MainCategory: Category{Name: "Main Category 1"},
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err := DB.Save(&post).Error; err != nil {
 | 
						if err := DB.Save(&post).Error; err != nil {
 | 
				
			||||||
@ -67,6 +66,14 @@ func TestHasOne(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Delete
 | 
						// Delete
 | 
				
			||||||
 | 
						DB.Model(&post).Association("Category").Delete(&category3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var category41 Category
 | 
				
			||||||
 | 
						DB.Model(&post).Related(&category41)
 | 
				
			||||||
 | 
						if category41.Name != "" {
 | 
				
			||||||
 | 
							t.Errorf("Category should be deleted with Delete")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Clear
 | 
						// Clear
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user