如何将类型为gorm.io/gorm.DB的变量用作github.com/jinzhu/gorm.DB?

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

How to use (variable of type gorm.io/gorm.DB) as github.com/jinzhu/gorm.DB?

问题

现在我已经将我的gorm包升级到了新版本,即"gorm.io/gorm",但我正在使用使用旧版本(github.com/jinzhu/gorm)的包(github.com/qor/admin)。

我需要将gorm.DB(新版本)的值传递给包"github.com/qor/admin"的一个函数,该函数将gorm.DB(旧版本)作为参数

package main

import (
	adminPkg "github.com/qor/admin"
	database "github.com/youssefsiam38/myfolder/db"
)

func main() {
	db, err := database.Connection() // 返回类型为*gorm.io/gorm.DB的db
	if err != nil {
		panic(err)
	}

	admin := adminPkg.New(&adminPkg.AdminConfig{DB: db})

}

错误信息:

vet: ./main.go:14:50: 无法将db(类型为gorm.DB的变量)作为结构体字面值中的gorm.DB值使用

英文:

Now I have upgraded my gorm package to the new version which is "gorm.io/gorm" but I am using package (github.com/qor/admin) that use the old version (github.com/jinzhu/gorm) of the package.

I need to pass gorm.DB(new version) value to a function of the package "github.com/qor/admin" that take gorm.DB(old version) as a parameter

package main

import (  
	adminPkg "github.com/qor/admin"
	database "github.com/youssefsiam38/myfolder/db"
)

func main() {
	db, err := database.Connection() // retrun db of type *gorm.io/gorm.DB
	if err != nil {
		panic(err)
	}

	admin := adminPkg.New(&adminPkg.AdminConfig{DB: db})

}

The err

vet: ./main.go:14:50: cannot use db (variable of type *gorm.DB) as *gorm.DB value in struct literal

答案1

得分: 0

你不能这样做。尽管名称和实现似乎表明它们有关联,但这两个对象实际上并没有关联。

github.com/qor/admin 库有一个开放的问题,所以我建议你保持关注并且可能参与迁移到 gorm 的新版本(如果 github.com/qor/admin 对你的操作很关键,也可以考虑回滚库的升级)。

需要注意的是,如果这些库使用了接口,第三方可能可以修复这个问题。孩子们要好好学习,多使用接口。

英文:

You can't. Those two objects are not related, even though the name and implementations seem to indicate otherwise.

The github.com/qor/admin library has an issue open for that, so I'd stay tuned and/or contribute to migrate to the new version of gorm (and maybe rollback the lib upgrade if github.com/qor/admin is critical for your operations 如何将类型为gorm.io/gorm.DB的变量用作github.com/jinzhu/gorm.DB?

It's good to note that if those libs were using interfaces, this could be fixable by third-parties. Stay in school kids, and use interfaces.

huangapple
  • 本文由 发表于 2021年7月17日 16:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/68418703.html
匿名

发表评论

匿名网友

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

确定