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

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

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

问题

<!--请勿更改模板-->

你的问题

你好
祝你有个愉快的一天

系统规格

  • 最新的go版本
  • gofiber
  • Windows 11
  • postgres

我试图运行AutoMigrate,但是出现了一个错误,之前一切都正常工作,但是自从上周以来,当我运行我的代码时,出现了以下错误。

  1. 2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
  2. ←[0m←[31m[error] ←[0m不支持的数据类型: &amp;[]
  3. 2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
  4. ←[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;[]
  5. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:192
  6. ←[0m←[31m[error] ←[0m不支持的数据类型: &amp;[]
  7. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
  8. ←[0m←[31m[error] ←[0m不支持的数据类型: &amp;[]
  9. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
  10. ←[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;[]
  11. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
  12. ←[0m←[31m[error] ←[0m不支持的数据类型: &amp;[]
  13. 抱歉无法迁移'...
  14. 数据库连接成功...

这是我的代码和出现问题的模型,因为它迁移了用户模型,然后在claims处卡住了。

  1. DBConnection
  2. var DB *gorm.DB
  3. func ConnectPg() {
  4. p := config.Config("DB_PORT")
  5. port, err := strconv.Atoi(p)
  6. // port, err := strconv.ParseUint(p, 10, 32)
  7. if err != nil {
  8. log.Println("Sorry db port error: ", err)
  9. }
  10. // connection url to DB
  11. 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"))
  12. // connect to DB
  13. var dbErr error
  14. DB, dbErr = gorm.Open(postgres.Open(dns), &gorm.Config{})
  15. if dbErr != nil {
  16. panic("failed to connect to database..")
  17. }
  18. fmt.Println("Running the migrations...")
  19. if migrateErr := DB.AutoMigrate(&models.User{}, &models.Claims{}, &models.Permission{}, &models.Role{}, &models.RolePermission{}, &models.SessionLog{}); migrateErr != nil {
  20. fmt.Println("Sorry couldn't migrate'...")
  21. }
  22. fmt.Println("Database connection was successful...")
  23. }
  1. Claims model
  2. package models
  3. import (
  4. "github.com/golang-jwt/jwt/v4"
  5. "github.com/google/uuid"
  6. )
  7. type Claims struct {
  8. jwt.RegisteredClaims
  9. ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
  10. }

这是我迁移到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.

  1. 2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
  2. ←[0m←[31m[error] ←[0munsupported data type: &amp;[]
  3. 2021/12/30 13:17:56 ←[35mC:/personal/projects/uni-blog/src/database/connect.go:43
  4. ←[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;[]
  5. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:192
  6. ←[0m←[31m[error] ←[0munsupported data type: &amp;[]
  7. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
  8. ←[0m←[31m[error] ←[0munsupported data type: &amp;[]
  9. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
  10. ←[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;[]
  11. 2021/12/30 13:17:56 ←[35mC:/Users/raliq/go/pkg/mod/gorm.io/driver/postgres@v1.1.2/migrator.go:167
  12. ←[0m←[31m[error] ←[0munsupported data type: &amp;[]
  13. Sorry couldn&#39;t migrate&#39;...
  14. 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

  1. DBConnection
  2. var DB *gorm.DB
  3. func ConnectPg() {
  4. p := config.Config(&quot;DB_PORT&quot;)
  5. port, err := strconv.Atoi(p)
  6. // port, err := strconv.ParseUint(p, 10, 32)
  7. if err != nil {
  8. log.Println(&quot;Sorry db port error: &quot;, err)
  9. }
  10. // connection url to DB
  11. 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;))
  12. // connect to DB
  13. var dbErr error
  14. DB, dbErr = gorm.Open(postgres.Open(dns), &amp;gorm.Config{})
  15. if dbErr != nil {
  16. panic(&quot;failed to connect to database..&quot;)
  17. }
  18. fmt.Println(&quot;Running the migrations...&quot;)
  19. if migrateErr := DB.AutoMigrate(&amp;models.User{}, &amp;models.Claims{}, &amp;models.Permission{}, &amp;models.Role{}, &amp;models.RolePermission{}, &amp;models.SessionLog{}); migrateErr != nil {
  20. fmt.Println(&quot;Sorry couldn&#39;t migrate&#39;...&quot;)
  21. }
  22. fmt.Println(&quot;Database connection was successful...&quot;)
  23. }
  1. Claims model
  2. package models
  3. import (
  4. &quot;github.com/golang-jwt/jwt/v4&quot;
  5. &quot;github.com/google/uuid&quot;
  6. )
  7. type Claims struct {
  8. jwt.RegisteredClaims
  9. ID uuid.UUID `gorm:&quot;type:uuid;default:uuid_generate_v4()&quot;`
  10. }

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:

确定