Support named Joins, close #3833
This commit is contained in:
		
							parent
							
								
									6a0fca2195
								
							
						
					
					
						commit
						e1952924e2
					
				| @ -108,7 +108,7 @@ func BuildQuerySQL(db *gorm.DB) { | ||||
| 			for _, join := range db.Statement.Joins { | ||||
| 				if db.Statement.Schema == nil { | ||||
| 					joins = append(joins, clause.Join{ | ||||
| 						Expression: clause.Expr{SQL: join.Name, Vars: join.Conds}, | ||||
| 						Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds}, | ||||
| 					}) | ||||
| 				} else if relation, ok := db.Statement.Schema.Relationships.Relations[join.Name]; ok { | ||||
| 					tableAliasName := relation.Name | ||||
| @ -150,7 +150,7 @@ func BuildQuerySQL(db *gorm.DB) { | ||||
| 					}) | ||||
| 				} else { | ||||
| 					joins = append(joins, clause.Join{ | ||||
| 						Expression: clause.Expr{SQL: join.Name, Vars: join.Conds}, | ||||
| 						Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds}, | ||||
| 					}) | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| @ -61,35 +61,41 @@ func TestJoinConds(t *testing.T) { | ||||
| 	DB.Save(&user) | ||||
| 
 | ||||
| 	var users1 []User | ||||
| 	DB.Joins("left join pets on pets.user_id = users.id").Where("users.name = ?", user.Name).Find(&users1) | ||||
| 	DB.Joins("inner join pets on pets.user_id = users.id").Where("users.name = ?", user.Name).Find(&users1) | ||||
| 	if len(users1) != 3 { | ||||
| 		t.Errorf("should find two users using left join, but got %v", len(users1)) | ||||
| 	} | ||||
| 
 | ||||
| 	var users2 []User | ||||
| 	DB.Joins("left join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Where("users.name = ?", user.Name).First(&users2) | ||||
| 	DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Where("users.name = ?", user.Name).First(&users2) | ||||
| 	if len(users2) != 1 { | ||||
| 		t.Errorf("should find one users using left join with conditions, but got %v", len(users2)) | ||||
| 	} | ||||
| 
 | ||||
| 	var users3 []User | ||||
| 	DB.Joins("left join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where("users.name = ?", user.Name).First(&users3) | ||||
| 	DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where("users.name = ?", user.Name).First(&users3) | ||||
| 	if len(users3) != 1 { | ||||
| 		t.Errorf("should find one users using multiple left join conditions, but got %v", len(users3)) | ||||
| 	} | ||||
| 
 | ||||
| 	var users4 []User | ||||
| 	DB.Joins("left join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number+"non-exist").Where("users.name = ?", user.Name).First(&users4) | ||||
| 	DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number+"non-exist").Where("users.name = ?", user.Name).First(&users4) | ||||
| 	if len(users4) != 0 { | ||||
| 		t.Errorf("should find no user when searching with unexisting credit card, but got %v", len(users4)) | ||||
| 	} | ||||
| 
 | ||||
| 	var users5 []User | ||||
| 	db5 := DB.Joins("left join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where(User{Model: gorm.Model{ID: 1}}).Where(Account{Model: gorm.Model{ID: 1}}).Not(Pet{Model: gorm.Model{ID: 1}}).Find(&users5) | ||||
| 	db5 := DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where(User{Model: gorm.Model{ID: 1}}).Where(Account{Model: gorm.Model{ID: 1}}).Not(Pet{Model: gorm.Model{ID: 1}}).Find(&users5) | ||||
| 	if db5.Error != nil { | ||||
| 		t.Errorf("Should not raise error for join where identical fields in different tables. Error: %s", db5.Error.Error()) | ||||
| 	} | ||||
| 
 | ||||
| 	var users6 []User | ||||
| 	DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = @Name", user.Pets[0]).Where("users.name = ?", user.Name).First(&users6) | ||||
| 	if len(users6) != 1 { | ||||
| 		t.Errorf("should find one users using left join with conditions, but got %v", len(users6)) | ||||
| 	} | ||||
| 
 | ||||
| 	dryDB := DB.Session(&gorm.Session{DryRun: true}) | ||||
| 	stmt := dryDB.Joins("left join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where(User{Model: gorm.Model{ID: 1}}).Where(Account{Model: gorm.Model{ID: 1}}).Not(Pet{Model: gorm.Model{ID: 1}}).Find(&users5).Statement | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu