go type_error ai_generated true

sql: converting argument $1 type: unsupported type []byte, a slice of bytes

ID: go/database-sql-unsupported-type

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-04-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.17 active
1.21 active

Root Cause

The database driver does not support a specific Go type passed as a query argument, such as []byte without proper conversion.

generic

中文

数据库驱动不支持作为查询参数传递的特定 Go 类型,例如未正确转换的 []byte。

Workarounds

  1. 90% success
    Convert []byte to a driver.Valuer: type ByteArray []byte; func (b ByteArray) Value() (driver.Value, error) { return []byte(b), nil }
  2. 80% success
    Use a driver-specific type like pq.ByteaArray for PostgreSQL: args = append(args, pq.ByteaArray(data))

Dead Ends

Common approaches that don't work:

  1. 60% fail

    May not preserve binary data and can cause encoding issues.

  2. 80% fail

    Doesn't solve the underlying type mismatch.