Fix delete many2many associations
This commit is contained in:
		
							parent
							
								
									ebe5f191a9
								
							
						
					
					
						commit
						b7554a2cb0
					
				@ -97,8 +97,12 @@ func (association *Association) Delete(values ...interface{}) *Association {
 | 
				
			|||||||
		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" {
 | 
				
			||||||
			whereSql := fmt.Sprintf("%v.%v IN (?)", relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey)))
 | 
								whereSql := fmt.Sprintf("%v.%v = ? AND %v.%v IN (?)",
 | 
				
			||||||
			association.Scope.db.Model("").Table(relationship.JoinTable).Where(whereSql, primaryKeys).Delete("")
 | 
									relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.ForeignKey)),
 | 
				
			||||||
 | 
									relationship.JoinTable, association.Scope.Quote(ToSnake(relationship.AssociationForeignKey)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								association.Scope.db.Model("").Table(relationship.JoinTable).
 | 
				
			||||||
 | 
									Where(whereSql, association.PrimaryKey, primaryKeys).Delete("")
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			association.err(errors.New("delete only support many to many"))
 | 
								association.err(errors.New("delete only support many to many"))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
package gorm_test
 | 
					package gorm_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "testing"
 | 
					import "testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "github.com/jinzhu/gorm"
 | 
					import "github.com/jinzhu/gorm"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Cat struct {
 | 
					type Cat struct {
 | 
				
			||||||
@ -207,11 +208,19 @@ func TestManyToMany(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	languages = []Language{}
 | 
						languages = []Language{}
 | 
				
			||||||
	DB.Where("name IN (?)", []string{"CC", "DD"}).Find(&languages)
 | 
						DB.Where("name IN (?)", []string{"CC", "DD"}).Find(&languages)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						user2 := User{Name: "Many2Many_User2", Languages: languages}
 | 
				
			||||||
 | 
						DB.Save(&user2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	DB.Model(&user).Association("Languages").Delete(languages, &languages)
 | 
						DB.Model(&user).Association("Languages").Delete(languages, &languages)
 | 
				
			||||||
	if DB.Model(&user).Association("Languages").Count() != len(totalLanguages)-3 {
 | 
						if DB.Model(&user).Association("Languages").Count() != len(totalLanguages)-3 {
 | 
				
			||||||
		t.Errorf("Relations should be deleted with Delete")
 | 
							t.Errorf("Relations should be deleted with Delete")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if DB.Model(&user2).Association("Languages").Count() == 0 {
 | 
				
			||||||
 | 
							t.Errorf("Other user's relations should not be deleted")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Replace
 | 
						// Replace
 | 
				
			||||||
	var languageB Language
 | 
						var languageB Language
 | 
				
			||||||
	DB.Where("name = ?", "BB").First(&languageB)
 | 
						DB.Where("name = ?", "BB").First(&languageB)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user