Home > Back-end >  Unsupported Scan, storing driver.Value type []uint8 into type *guid.GUID
Unsupported Scan, storing driver.Value type []uint8 into type *guid.GUID

Time:09-06

I work with Golang and SQL Server. My struct in Golang:

type Role struct {
Id          guid.GUID `gorm:"primaryKey;column:Id;type:uniqueidentifier" json:"id"`
RoleName    string    `gorm:"column:RoleName;not null;unique" json:"roleName"`
IsEnable    bool      `gorm:"column:IsEnable" json:"isEnable"`
Permissions []RolePermission }

I use gorm to query data but receive error:

unsupported Scan, storing driver.Value type []uint8 into type *guid.GUID.

I used uuid before but the id data is wrong when query (guid to uuid).

Is any way to store and work with Guid using Golang and SQL server

CodePudding user response:

Early versions of go-gorm (v0.2) were including UUID/GUID support for SQLTag, with isUUID() a test on the type name ("uuid" or "guid").

But that code is no longer present in current go-gorm v2.0.

You might need to implement a custom Data Type Scanner / Valuer, or use one like google/uuid:

import (
  "github.com/google/uuid"
  "github.com/lib/pq"
)

type Post struct {
  ID     uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
  Title  string
  Tags   pq.StringArray `gorm:"type:text[]"`
}
  • Related