jmoiron/sqlx · error

bad []byte type assertion

Error message

bad []byte type assertion

What it means

BitBool.Scan implements sql.Scanner assuming MySQL's BIT type, which arrives as a []byte whose first byte indicates the boolean value. If the driver value is not []byte, the assertion fails and this error is returned. It cannot handle string, int64, or bool sources.

Source

Thrown at types/types.go:169

// BitBool is an implementation of a bool for the MySQL type BIT(1).
// This type allows you to avoid wasting an entire byte for MySQL's boolean type TINYINT.
type BitBool bool

// Value implements the driver.Valuer interface,
// and turns the BitBool into a bitfield (BIT(1)) for MySQL storage.
func (b BitBool) Value() (driver.Value, error) {
	if b {
		return []byte{1}, nil
	}
	return []byte{0}, nil
}

// Scan implements the sql.Scanner interface,
// and turns the bitfield incoming from MySQL into a BitBool
func (b *BitBool) Scan(src interface{}) error {
	v, ok := src.([]byte)
	if !ok {
		return errors.New("bad []byte type assertion")
	}
	*b = v[0] == 1
	return nil
}

View on GitHub (pinned to 41dac167fd)

Solutions

  1. Use BitBool only with MySQL BIT columns returned as []byte.
  2. For other types/databases, scan into a bool or implement a custom Scanner that handles bool/int64/[]byte.
  3. Check driver DSN/config so BIT columns come back as []byte.
  4. Cast in SQL: SELECT CAST(col AS CHAR) or col+0 handled appropriately for your type.

Example fix

// before
var b types.BitBool
row.Scan(&b) // Postgres bool
// after
var b bool
row.Scan(&b)
Defensive patterns

Strategy: type-guard

Validate before calling

func bitBoolScannable(src interface{}) bool {
	_, ok := src.([]byte)
	return ok
}

Type guard

func asBytes(v interface{}) ([]byte, bool) {
	b, ok := v.([]byte)
	return b, ok
}

Try / catch

var b types.BitBool
if err := row.Scan(&b); err != nil {
	if err.Error() == "bad []byte type assertion" {
		// fall back to a bool/int64 scan path for non-MySQL drivers
	}
	return err
}

Prevention

When it happens

Trigger: Scanning a BIT/boolean column into BitBool when the driver returns non-[]byte: e.g. MySQL bit(1) returned as uint64/int64 by some drivers or configs, or Postgres boolean returned as bool.

Common situations: Using BitBool with non-MySQL drivers (Postgres bool arrives as bool, not []byte); driver settings like interpolateParams/DSN flags changing BIT representation; using BitBool on an INTEGER column.

Related errors


AI-assisted analysis of jmoiron/sqlx@41dac167fd (2026-09-03). Data as JSON: /api/errors/ff8731b61862e220. Report an issue: GitHub.