【发布时间】: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 令牌
请帮忙
【问题讨论】:
-
@github.com/longlihale 说
RegisteredClaims.Audience is []string data type, gorm not supported this data type. so you can't use it directly .这里github.com/go-gorm/gorm/issues/4972
标签: postgresql go jwt go-gorm fibers