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

Viewed 243

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 Answers

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 :)

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.

Related