英文:
got error unsupported data type: &[]. this is gorm model + golang/jwt
问题
<!--请勿更改模板-->
你的问题
你好
祝你有个愉快的一天
系统规格
- 最新的go版本
- gofiber
- Windows 11
- postgres
我试图运行AutoMigrate,但是出现了一个错误,之前一切都正常工作,但是自从上周以来,当我运行我的代码时,出现了以下错误。
2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
←[0m←[31m[error] ←[0m不支持的数据类型: &[]
2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
←[0m←[31m[error] ←[0m无法解析值 &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}}, 出现错误: 不支持的数据类型: &[]
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] ←[0m不支持的数据类型: &[]
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] ←[0m不支持的数据类型: &[]
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] ←[0m无法解析值 &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}}, 出现错误: 不支持的数据类型: &[]
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] ←[0m不支持的数据类型: &[]
抱歉无法迁移'...
数据库连接成功...
这是我的代码和出现问题的模型,因为它迁移了用户模型,然后在claims处卡住了。
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之前的原始代码,我只是想做与之前相同的事情,但它不起作用。
<!--你想要什么-->
我更改了jwt包,使用了golang/jwt v4,现在我正在尝试迁移我的代码以适应这个新变化,但是模型中有些问题。我想生成用于访问和刷新令牌的jwt令牌
请帮忙
英文:
<!-- DON'T CHANGE THE TEMPLATE -->
Your Question
Hello
good day
System specs
- go latest
- gofiber
- windows 11
- postgres
Am trying to run AutoMigrate but am having an error, everything was working fine but since last week i get the following error when i run my code.
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...
this is my code and the model where the problem appears, because it migrates the user model then it gets stuck at claims
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()"`
}
Here is the original code before i migrated to https://github.com/golang-jwt/jwt from https://github.com/dgrijalva/jwt-go. here is the repo i just want to do the same thing as before but its not working.
<!-- What you want -->
I changed jwt packages using the golang/jwt v4 now am trying to migrate my code to work with the new change but something is wrong in the models. I want to generate jwt token for access and refresh tokens
please help
答案1
得分: 1
Scanner/Valuer接口没有为切片类型(如[]string)实现。因此,您可以使用https://pkg.go.dev/github.com/lib/pq中的pq.StringArray类型,而不是jwt.RegisteredClaims结构中的[]string类型。
您可以使用自定义结构,该结构具有相同的字段,但使用pq.StringArray类型而不是[]string类型。
英文:
The Scanner/Valuer interface is not implemented for slice types ie []string. So you can use the pq.StringArray type from https://pkg.go.dev/github.com/lib/pq instead of the []string type in the jwt.RegisteredClaims struct.
You can use a custom struct that have the same fields but with the pq.StringArray type instead of []string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论