Merge pull request #695 from henriquemenezes/master
Fix where clause for string primary key when query value is numeric and starts with zero
This commit is contained in:
		
						commit
						96f2bc40c7
					
				@ -64,6 +64,22 @@ func TestUIntPrimaryKey(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestStringPrimaryKeyForNumericValueStartingWithZero(t *testing.T) {
 | 
				
			||||||
 | 
						type AddressByZipCode struct {
 | 
				
			||||||
 | 
							ZipCode   string `gorm:"primary_key"`
 | 
				
			||||||
 | 
							Address   string
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						DB.AutoMigrate(&AddressByZipCode{})
 | 
				
			||||||
 | 
						DB.Create(&AddressByZipCode{ZipCode: "00501", Address: "Holtsville"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var address AddressByZipCode
 | 
				
			||||||
 | 
						DB.First(&address, "00501")
 | 
				
			||||||
 | 
						if address.ZipCode != "00501" {
 | 
				
			||||||
 | 
							t.Errorf("Fetch a record from with a string primary key for a numeric value starting with zero should work, but failed")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestFindAsSliceOfPointers(t *testing.T) {
 | 
					func TestFindAsSliceOfPointers(t *testing.T) {
 | 
				
			||||||
	DB.Save(&User{Name: "user"})
 | 
						DB.Save(&User{Name: "user"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -19,8 +19,7 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri
 | 
				
			|||||||
	case string:
 | 
						case string:
 | 
				
			||||||
		// if string is number
 | 
							// if string is number
 | 
				
			||||||
		if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
 | 
							if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
 | 
				
			||||||
			id, _ := strconv.Atoi(value)
 | 
								return scope.primaryCondition(scope.AddToVars(value))
 | 
				
			||||||
			return scope.primaryCondition(scope.AddToVars(id))
 | 
					 | 
				
			||||||
		} else if value != "" {
 | 
							} else if value != "" {
 | 
				
			||||||
			str = fmt.Sprintf("(%v)", value)
 | 
								str = fmt.Sprintf("(%v)", value)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user