Skip to content

Commit

Permalink
Fix where clause for string primary key when query value is numeric a…
Browse files Browse the repository at this point in the history
…nd starts with zero
  • Loading branch information
henriquemenezes committed Oct 16, 2015
1 parent 20e37a0 commit e68fb8f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 16 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
DB.Save(&User{Name: "user"})

Expand Down
3 changes: 1 addition & 2 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri
case string:
// if string is number
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
id, _ := strconv.Atoi(value)
return scope.primaryCondition(scope.AddToVars(id))
return scope.primaryCondition(scope.AddToVars(value))
} else if value != "" {
str = fmt.Sprintf("(%v)", value)
}
Expand Down

0 comments on commit e68fb8f

Please sign in to comment.