import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"server/config"
"gorm.io/plugin/dbresolver"
)
func DB(config *config.Config) {
var err error
config.DB, err = gorm.Open("mysql", config.DBDSN)
if err != nil {
panic(err)
}
if !config.IsDev {
config.DB.Use(dbresolver.Register(dbresolver.Config{
Replicas: []gorm.Dialector{mysql.Open("mysql", config.DBDSN2)},
}))
}
}
I am trying to use GORM's DBresolver to make use of my cloud SQL instance's read replica. I think there is some versioning issue with the GORM package that I use and the DBresolver plugin. When I run the code above I get the following error:
- config.DB.Use undefined (type *"github.com/jinzhu/gorm".DB has no field or method Use)
- undefined: "github.com/jinzhu/gorm".Dialect
I could not find any reason for this online and there are very few online resources on GORM's advanced functionalities. I can easily get the replica to work if I create a separate connection to it, but that way I need to specify the DB every-time I interact with the database. I used the following to implement the code above: https://gorm.io/docs/dbresolver.html