Gin - Cannot use 'xxx' (type string) as the type Dialector Type

发布于 2022-02-21  817 次阅读


内容纲要

完整报错如下:

Cannot use 'xxx' (type string) as the type Dialector Type does not implement 'Dialector' as some methods are missing: Name() string Initialize(DB) error Migrator(db DB) Migrator ...

这一段本来应该是填driverName或者类似的参数,但是在Gin对Open函数做了改动之后,新的写法变成了这样:

dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})

如果mysql提示不存在,需要引入下面这个库:

go get -u "gorm.io/driver/mysql"

官方文档: https://gorm.io/zh_CN/docs/connecting_to_the_database.html


不爱嘉然小姐十年了。十年里,爱过的每个人都像她。