{"record":{"id":"ff8731b61862e220","repo":"jmoiron/sqlx","slug":"bad-byte-type-assertion","errorCode":null,"errorMessage":"bad []byte type assertion","messagePattern":"bad \\[\\]byte type assertion","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"types/types.go","lineNumber":169,"sourceCode":"// BitBool is an implementation of a bool for the MySQL type BIT(1).\n// This type allows you to avoid wasting an entire byte for MySQL's boolean type TINYINT.\ntype BitBool bool\n\n// Value implements the driver.Valuer interface,\n// and turns the BitBool into a bitfield (BIT(1)) for MySQL storage.\nfunc (b BitBool) Value() (driver.Value, error) {\n\tif b {\n\t\treturn []byte{1}, nil\n\t}\n\treturn []byte{0}, nil\n}\n\n// Scan implements the sql.Scanner interface,\n// and turns the bitfield incoming from MySQL into a BitBool\nfunc (b *BitBool) Scan(src interface{}) error {\n\tv, ok := src.([]byte)\n\tif !ok {\n\t\treturn errors.New(\"bad []byte type assertion\")\n\t}\n\t*b = v[0] == 1\n\treturn nil\n}\n","sourceCodeStart":151,"sourceCodeEnd":174,"githubUrl":"https://github.com/jmoiron/sqlx/blob/41dac167fdad5e3fd81d66cafba0951dc6823a30/types/types.go#L151-L174","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use BitBool only with MySQL BIT columns returned as []byte.","For other types/databases, scan into a bool or implement a custom Scanner that handles bool/int64/[]byte.","Check driver DSN/config so BIT columns come back as []byte.","Cast in SQL: SELECT CAST(col AS CHAR) or col+0 handled appropriately for your type."],"exampleFix":"// before\nvar b types.BitBool\nrow.Scan(&b) // Postgres bool\n// after\nvar b bool\nrow.Scan(&b)","handlingStrategy":"type-guard","validationCode":"func bitBoolScannable(src interface{}) bool {\n\t_, ok := src.([]byte)\n\treturn ok\n}","typeGuard":"func asBytes(v interface{}) ([]byte, bool) {\n\tb, ok := v.([]byte)\n\treturn b, ok\n}","tryCatchPattern":"var b types.BitBool\nif err := row.Scan(&b); err != nil {\n\tif err.Error() == \"bad []byte type assertion\" {\n\t\t// fall back to a bool/int64 scan path for non-MySQL drivers\n\t}\n\treturn err\n}","preventionTips":["Use BitBool only for MySQL BIT columns.","With Postgres/other drivers, scan into bool instead.","Confirm driver version returns []byte for BIT.","Add a driver smoke test scanning one BitBool row at startup."],"tags":["sql","scan","mysql","bit","type-assertion"],"backgroundTag":"bad-type-assertion","analyzedSha":"41dac167fdad5e3fd81d66cafba0951dc6823a30","analyzedAt":"2026-09-03T06:10:20.382Z","contentChangedAt":"2026-09-03T06:10:20.382Z","schemaVersion":2},"datasetVersion":"2026-09-10T12:17:11.382Z"}