【问题标题】:got error unsupported data type: &[]. this is gorm model + golang/jwt得到错误不支持的数据类型:&[]。这是 gorm 模型 + golang/jwt
【发布时间】:2021-12-31 09:01:29
【问题描述】:

您的问题

你好
美好的一天

系统规格

  • 最新版
  • gofiber
  • Windows 11
  • postgres

我正在尝试运行 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/dgrijalva/jwt-go 迁移到https://github.com/golang-jwt/jwt 之前的原始代码。 here is the repo 我只想做和以前一样的事情,但它不起作用。

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

请帮忙

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

【问题讨论】:

标签: postgresql go jwt go-gorm fibers


【解决方案1】:

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

【讨论】:

  • 谢谢,我确实创建了一个自定义卡住并创建了一个具有 jwt.RegisteredClaims 的单独结构。
  • 没问题。这是一种享受。
猜你喜欢
  • 2017-06-21
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多