收到,请稍等片刻,我会为您进行翻译。

huangapple go评论94阅读模式
英文:

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不支持的数据类型: &amp;[]

2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
←[0m←[31m[error] ←[0m无法解析值 &amp;models.Claims{RegisteredClaims:jwt.RegisteredClaims{Issuer:&quot;&quot;, Subject:&quot;&quot;, Audience:jwt.ClaimStrings(nil), ExpiresAt:&lt;nil&gt;, NotBefore:&lt;nil&gt;, IssuedAt:&lt;nil&gt;, ID:&quot;&quot;}, ID:uuid.UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, 出现错误: 不支持的数据类型: &amp;[]

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不支持的数据类型: &amp;[]

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不支持的数据类型: &amp;[]

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无法解析值 &amp;models.Claims{RegisteredClaims:jwt.RegisteredClaims{Issuer:&quot;&quot;, Subject:&quot;&quot;, Audience:jwt.ClaimStrings(nil), ExpiresAt:&lt;nil&gt;, NotBefore:&lt;nil&gt;, IssuedAt:&lt;nil&gt;, ID:&quot;&quot;}, ID:uuid.UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, 出现错误: 不支持的数据类型: &amp;[]

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不支持的数据类型: &amp;[]
抱歉无法迁移'...
数据库连接成功...

这是我的代码和出现问题的模型,因为它迁移了用户模型,然后在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: &amp;[]

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

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: &amp;[]

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: &amp;[]

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 &amp;models.Claims{RegisteredClaims:jwt.RegisteredClaims{Issuer:&quot;&quot;, Subject:&quot;&quot;, Audience:jwt.ClaimStrings(nil), ExpiresAt:&lt;nil&gt;, NotBefore:&lt;nil&gt;, IssuedAt:&lt;nil&gt;, ID:&quot;&quot;}, ID:uuid.UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, got error unsupported data type: &amp;[]

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: &amp;[]
Sorry couldn&#39;t migrate&#39;...
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(&quot;DB_PORT&quot;)
	port, err := strconv.Atoi(p)
	// port, err := strconv.ParseUint(p, 10, 32)
	if err != nil {
		log.Println(&quot;Sorry db port error: &quot;, err)
	}

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

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

	if dbErr != nil {
		panic(&quot;failed to connect to database..&quot;)
	}

	fmt.Println(&quot;Running the migrations...&quot;)

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

	fmt.Println(&quot;Database connection was successful...&quot;)
}
Claims model

package models

import (
	&quot;github.com/golang-jwt/jwt/v4&quot;
	&quot;github.com/google/uuid&quot;
)

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

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

Thanks in advance
How i used it to save the audience
收到,请稍等片刻,我会为您进行翻译。

答案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.

huangapple
  • 本文由 发表于 2021年12月31日 17:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/70540637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定