jackc/pgx · error

cannot scan NULL to string

Error message

cannot scan NULL to string

What it means

ScanHstore hit an hstore key whose value is NULL while building a map[string]string, which cannot store NULL values. The k=v pair with NULL value is the faulting input; use map[string]*string to accept NULL hstore values.

Solutions

  1. Scan into map[string]*string or pgtype.Hstore, which support NULL values
  2. Fill NULLs in the query side before returning the hstore
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

Thrown at pgtype/builtin_wrappers.go:627

type mapStringToPointerStringWrapper map[string]*string

func (w *mapStringToPointerStringWrapper) ScanHstore(v Hstore) error {
	*w = mapStringToPointerStringWrapper(v)
	return nil
}

func (w mapStringToPointerStringWrapper) HstoreValue() (Hstore, error) {
	return Hstore(w), nil
}

type mapStringToStringWrapper map[string]string

func (w *mapStringToStringWrapper) ScanHstore(v Hstore) error {
	*w = make(mapStringToStringWrapper, len(v))
	for k, v := range v {
		if v == nil {
			return fmt.Errorf("cannot scan NULL to string")
		}
		(*w)[k] = *v
	}
	return nil
}

func (w mapStringToStringWrapper) HstoreValue() (Hstore, error) {
	if w == nil {
		return nil, nil
	}

	hstore := make(Hstore, len(w))
	for k, v := range w {
		s := v
		hstore[k] = &s
	}
	return hstore, nil
}

View on GitHub (pinned to ec1a0befd2)