1.报错
/github.com/casbin/gorm-adapter/v3@v3.13.0/adapter.go:419 Error 1071: Specified key was too long; max key length is 1000 bytes
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x60 pc=0x12ac3c0]
2.解决办法
我试着将数据库中表的类型变为InnoDB确实解决了问题,gorm默认的引擎是MyISAM,为了直接创建innoDB类型的表,于是我将连接数据库部分按照官网做出如下更改
Db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&gormadapter.CasbinRule{})
完整连接数据库
func DbConnect() *gorm.DB{
mysqlConf := GetConfigData()
DataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=true&loc=Local",
mysqlConf.MysqlConf.UserName,
mysqlConf.MysqlConf.Password,
mysqlConf.MysqlConf.Host,
mysqlConf.MysqlConf.Port,
mysqlConf.MysqlConf.DataBase)
Db, err := gorm.Open(mysql.Open(DataSource), &gorm.Config{})
Db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&gormadapter.CasbinRule{})
if err != nil {
log.Fatal(err)
}
mysqlDB,err:=Db.DB()
if err != nil {
log.Fatal(err)
}
mysqlDB.SetMaxIdleConns(5)
mysqlDB.SetMaxOpenConns(10)
return Db
}