jackc/pgx · error

cannot scan NULL into *uint

Error message

cannot scan NULL into *uint

What it means

Returned by uintWrapper.ScanNumeric when the PostgreSQL numeric is NULL (Valid==false) and the destination *uint has no way to represent NULL. Unlike pointer or pgtype wrapper targets, a plain uint cannot store null.

Solutions

  1. Change the query to use COALESCE with a default value
  2. Scan into *uint only for NOT NULL columns
  3. Use a nullable target such as pgtype.Numeric or a *uint pointer
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/builtin_wrappers.go:282 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/cec5b3f76ecf35e9. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/builtin_wrappers.go:282

		return fmt.Errorf("%d is greater than maximum value for uint", v.Int64)
	}

	*w = uintWrapper(v.Int64)

	return nil
}

func (w uintWrapper) Int64Value() (Int8, error) {
	if uint64(w) > uint64(math.MaxInt64) {
		return Int8{}, fmt.Errorf("%d is greater than maximum value for int64", w)
	}

	return Int8{Int64: int64(w), Valid: true}, nil
}

func (w *uintWrapper) ScanNumeric(v Numeric) error {
	if !v.Valid {
		return fmt.Errorf("cannot scan NULL into *uint")
	}

	bi, err := v.toBigInt()
	if err != nil {
		return fmt.Errorf("cannot scan into *uint: %w", err)
	}

	if !bi.IsUint64() {
		return fmt.Errorf("cannot scan %v into *uint", bi.String())
	}

	ui := bi.Uint64()

	if math.MaxUint < ui {
		return fmt.Errorf("cannot scan %v into *uint", ui)
	}

	*w = uintWrapper(ui)

View on GitHub (pinned to ec1a0befd2)