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

Time:01-03

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/[email protected]/migrator.go:192
←[0m←[31m[error] ←[0munsupported data type: &[]

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

2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/[email protected]/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/[email protected]/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 enter image description here

CodePudding user response:

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.

  • Related