Can anyone explain the difference between Gorm v1 and Gorm v2 using dialects?
I've been reusing code from the previous project, which worked with Gorm v1, where it was querying the dialect gorm.GetDialect()
from dsn.Hostname, and applying it via gorm.RegisterDialect()
.
There are no such methods in Gorm 2, and I'm puzzled about what these are for anyways.
Seems that if these has been removed, then we don't need them anyways, right?
CodePudding user response:
For Gorm V2, the DB connection is changed to
import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
The code of gorm.Open
initialize db session based on dialector, which is replaced with gorm.RegisterDialect()
// Open initialize db session based on dialector
func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
config := &Config{}
More V2 API details please refer to https://gorm.io/docs/