jackc/pgx · error

cannot scan NULL into *net.IPNet

Error message

cannot scan NULL into *net.IPNet

What it means

ScanNetipPrefix was given an invalid (zero-value, NULL) netip.Prefix while scanning into *net.IPNet, which cannot represent NULL. Null guard in netIPNetWrapper; the inet/cidr column was NULL but the destination is a non-nullable net.IPNet.

Solutions

  1. Scan into netip.Prefix (its zero value represents NULL) or *net.IPNet
  2. Use COALESCE with a default network in the query
  3. Ensure the column is NOT NULL
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

Thrown at pgtype/builtin_wrappers.go:520

func (w *durationWrapper) ScanInterval(v Interval) error {
	if !v.Valid {
		return fmt.Errorf("cannot scan NULL into *time.Interval")
	}

	us := int64(v.Months)*microsecondsPerMonth + int64(v.Days)*microsecondsPerDay + v.Microseconds
	*w = durationWrapper(time.Duration(us) * time.Microsecond)
	return nil
}

func (w durationWrapper) IntervalValue() (Interval, error) {
	return Interval{Microseconds: int64(w) / 1000, Valid: true}, nil
}

type netIPNetWrapper net.IPNet

func (w *netIPNetWrapper) ScanNetipPrefix(v netip.Prefix) error {
	if !v.IsValid() {
		return fmt.Errorf("cannot scan NULL into *net.IPNet")
	}

	*w = netIPNetWrapper{
		IP:   v.Addr().AsSlice(),
		Mask: net.CIDRMask(v.Bits(), v.Addr().BitLen()),
	}

	return nil
}

func (w netIPNetWrapper) NetipPrefixValue() (netip.Prefix, error) {
	ip, ok := netip.AddrFromSlice(w.IP)
	if !ok {
		return netip.Prefix{}, errors.New("invalid net.IPNet")
	}

	ones, _ := w.Mask.Size()

View on GitHub (pinned to ec1a0befd2)