Skip to content

MySQL Scan Error: Unsupported scan type \[]uint8 #14

Open
@voltaSerhiy

Description

@voltaSerhiy

MySQL Scan Error: Unsupported scan type []uint8

Description

When using typeid-go v1.3.0 with MySQL, I encountered the following error while scanning TypeID values:

sql: Scan error on column index 0, name "id": unsupported scan type []uint8

This appears to be because the MySQL driver returns []byte (which shows up as []uint8) for string-based columns like VARCHAR or TEXT, but the current Scan method only handles nil and string.

Local Fix

To work around this, I added support for []byte in the Scan method like so:

func (tid *TypeID[P]) Scan(src any) error {
	switch obj := src.(type) {
	case nil:
		return nil
	case []byte:
		if len(obj) == 0 {
			return nil
		}
		return tid.UnmarshalText(obj)
	case string:
		if src == "" {
			return nil
		}
		return tid.UnmarshalText([]byte(obj))
	default:
		return fmt.Errorf("unsupported scan type %T", obj)
	}
}

This resolved the issue in my case with MySQL.

Suggestion

Would it make sense to include []byte support in the Scan method to improve compatibility with MySQL and potentially other drivers that behave similarly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions