jackc/pgx · error

cannot convert to int64

Error message

cannot convert %v to int64

What it means

Numeric.Int64Value converts a valid Numeric to an Int8 by first reducing it to a big.Int. This guard fires when that big integer does not fit in an int64 — the numeric value has a magnitude or scale outside int64 range, so a lossless conversion is impossible.

Solutions

  1. Keep using Numeric as the Go type instead of converting to int64
  2. Round or clamp the value before conversion if lossiness is acceptable
  3. Check the column's numeric precision/scale and the magnitudes of stored values
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pgtype/numeric.go:124 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/9443a363f19332d5. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/numeric.go:124

	}

	*n = Numeric{Int: big.NewInt(v.Int64), Valid: true}
	return nil
}

// Int64Value implements the [Int64Valuer] interface.
func (n Numeric) Int64Value() (Int8, error) {
	if !n.Valid {
		return Int8{}, nil
	}

	bi, err := n.toBigInt()
	if err != nil {
		return Int8{}, err
	}

	if !bi.IsInt64() {
		return Int8{}, fmt.Errorf("cannot convert %v to int64", n)
	}

	return Int8{Int64: bi.Int64(), Valid: true}, nil
}

func (n *Numeric) ScanScientific(src string) error {
	if !strings.ContainsAny(src, "eE") {
		return scanPlanTextAnyToNumericScanner{}.Scan([]byte(src), n)
	}

	if bigF, ok := new(big.Float).SetString(src); ok {
		smallF, _ := bigF.Float64()
		src = strconv.FormatFloat(smallF, 'f', -1, 64)
	}

	num, exp, err := parseNumericString(src)
	if err != nil {
		return err

View on GitHub (pinned to ec1a0befd2)