DB.Joins("left join pets on pets.user_id = users.id").Where("users.name = ?",user.Name).Find(&users1)
iflen(users1)!=3{
t.Errorf("should find two users using left join, but got %v",len(users1))
}
varusers2[]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)
iflen(users2)!=1{
t.Errorf("should find one users using left join with conditions, but got %v",len(users2))
}
varusers3[]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)
iflen(users3)!=1{
t.Errorf("should find one users using multiple left join conditions, but got %v",len(users3))
}
varusers4[]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)
iflen(users4)!=0{
t.Errorf("should find no user when searching with unexisting credit card, but got %v",len(users4))
}
varusers5[]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)
ifdb5.Error!=nil{
t.Errorf("Should not raise error for join where identical fields in different tables. Error: %s",db5.Error.Error())