diff --git a/query_test.go b/query_test.go index 0fd58302..274e8e9b 100644 --- a/query_test.go +++ b/query_test.go @@ -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"}) diff --git a/scope_private.go b/scope_private.go index 16a96628..1c6e84b1 100644 --- a/scope_private.go +++ b/scope_private.go @@ -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) }