go type_error ai_generated true

sql: 转换参数 $1 类型:不支持的类型 []byte,字节切片

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

ID: go/database-sql-unsupported-type

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2024-04-02首次发现

版本兼容性

版本状态引入弃用备注
1.17 active
1.21 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 60% 失败

    May not preserve binary data and can cause encoding issues.

  2. 80% 失败

    Doesn't solve the underlying type mismatch.