我使用 Golang 和 SQL Server。我在 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 }
我使用 gorm 查詢資料但收到錯誤:
不支持掃描,將 driver.Value 型別 []uint8 存盤到 *guid.GUID 型別中。
我之前用過uuid,但是查詢時id資料不對(guid to uuid)。
是使用 Golang 和 SQL 服務器存盤和使用 Guid 的任何方式嗎
uj5u.com熱心網友回復:
go-gorm ( v0.2 ) 的早期版本包括對 SQLTag 的 UUID/GUID 支持,并對isUUID()型別名稱(“uuid”或“guid”)進行了測驗。
但是該代碼不再存在于當前的 go-gorm v2.0中。
您可能需要實作自定義 Data Type Scanner / Valuer,或使用類似的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[]"`
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/504868.html
