收到错误不支持的数据类型:&[]。这是 gorm 模型 + golang/jwt

got error unsupported data type: &[]. this is gorm model + golang/jwt

你的问题

你好
美好的一天

系统规格

我正在尝试 运行 AutoMigrate,但出现错误,一切正常,但自上周以来,当我 运行 我的代码时出现以下错误。

2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
←[0m←[31m[error] ←[0munsupported data type: &[]

2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
←[0m←[31m[error] ←[0mfailed to parse value &models.Claims{RegisteredClaims:jwt.RegisteredClaims{Issuer:"", Subject:"", Audience:jwt.ClaimStrings(nil), ExpiresAt:<nil>, NotBefore:<nil>, IssuedAt:<nil>, ID:""}, ID:uuid.UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, got error unsupported data type: &[]

2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:192
←[0m←[31m[error] ←[0munsupported data type: &[]

2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
←[0m←[31m[error] ←[0munsupported data type: &[]

2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
←[0m←[31m[error] ←[0mfailed to parse value &models.Claims{RegisteredClaims:jwt.RegisteredClaims{Issuer:"", Subject:"", Audience:jwt.ClaimStrings(nil), ExpiresAt:<nil>, NotBefore:<nil>, IssuedAt:<nil>, ID:""}, ID:uuid.UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, got error unsupported data type: &[]

2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
←[0m←[31m[error] ←[0munsupported data type: &[]
Sorry couldn't migrate'...
Database connection was successful...

这是我的代码和出现问题的模型,因为它迁移了用户模型然后卡在声明中

DBConnection

var DB *gorm.DB

func ConnectPg() {
    p := config.Config("DB_PORT")
    port, err := strconv.Atoi(p)
    // port, err := strconv.ParseUint(p, 10, 32)
    if err != nil {
        log.Println("Sorry db port error: ", err)
    }

    // connection url to DB
    dns := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", config.Config("DB_HOST"), port, config.Config("DB_USER"), config.Config("DB_PASSWORD"), config.Config("DB_NAME"))

    // connect to DB
    var dbErr error
    DB, dbErr = gorm.Open(postgres.Open(dns), &gorm.Config{})

    if dbErr != nil {
        panic("failed to connect to database..")
    }

    fmt.Println("Running the migrations...")

    if migrateErr := DB.AutoMigrate(&models.User{}, &models.Claims{}, &models.Permission{}, &models.Role{}, &models.RolePermission{}, &models.SessionLog{}); migrateErr != nil {
        fmt.Println("Sorry couldn't migrate'...")
    }

    fmt.Println("Database connection was successful...")
}
Claims model

package models

import (
    "github.com/golang-jwt/jwt/v4"
    "github.com/google/uuid"
)

type Claims struct {
    jwt.RegisteredClaims
    ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
}

这是我迁移到 https://github.com/golang-jwt/jwt from https://github.com/dgrijalva/jwt-go. here is the repo 之前的原始代码,我只想做和以前一样的事情,但它不起作用。

我使用 golang/jwt v4 更改了 jwt 包,现在正在尝试迁移我的代码以适应新的更改,但模型中出现了问题。我想为访问和刷新令牌生成 jwt 令牌

请帮忙

提前致谢 我是如何用它来拯救观众的

Scanner/Valuer 接口未针对切片类型实现,即 []string。因此,您可以使用 https://pkg.go.dev/github.com/lib/pq 中的 pq.StringArray 类型,而不是 jwt.RegisteredClaims 结构中的 []string 类型。 您可以使用具有相同字段但具有 pq.StringArray 类型而不是 []string.

的自定义结构