Golang使用GORM时,尝试迁移时出现sqlite错误。

huangapple go评论137阅读模式
英文:

Golang with GORM - sqlite Error when trying to migrate

问题

我正在开发一个机器人。为此,我决定使用sqlite数据库和GORM作为ORM。现在我正在处理一个简单的"Connection"文件,它应该连接到sqlite数据库(与Conection.go文件位于同一文件夹中)并迁移预定义的结构。

package database

import (
	_ "github.com/mattn/go-sqlite3"
	GlobalModels "goBot/modules/global/models"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"log"
	"os"
)

func Connect() *gorm.DB {

	log.Print(os.Getenv("DB_PATH"))

	db, err := gorm.Open(sqlite.Open("db.sqlite"), &gorm.Config{})
	if err != nil {
		log.Fatal(err)
	}

	return db
}

func Migrate() {

	db := Connect()

	errUser := db.AutoMigrate(GlobalModels.User{})
	if errUser != nil {
		log.Fatal(errUser)
	}

	errUserFlags := db.AutoMigrate(GlobalModels.UserFlags{})
	if errUserFlags != nil {
		log.Fatal(errUserFlags)
	}
}

以下是预定义的结构User.go和UserFlags.go:

package models

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Name   string `gorm:"column:name"`
	UserID int    `gorm:"column:user_id"`
	ChatID string `gorm:"column:chat_id"`
}


package models

import "gorm.io/gorm"

type UserFlags struct {
	gorm.Model
	UserID           int
	LastModuleChosen string `gorm:"column:last_module_chosen"`
	User             User   `gorm:"foreignKey:UserID"`
}

这是主函数,我在其中尝试迁移这些模型:

func main() {

	database.Migrate()

	fmt.Println("Starting the bot...")
	startBot()
}

当我运行main.go时,出现以下两个错误:
....\go\pkg\mod\gorm.io\driver\sqlite@v1.5.0\error_translator.go:9:35: undefined: sqlite3.ErrNoExtended
....\go\pkg\mod\gorm.io\driver\sqlite@v1.5.0\error_translator.go:14:36: undefined: sqlite3.Error

我确保sqlite3已设置为PATH环境变量,并且我还安装了sqlite3的golang驱动程序"github.com/mattn/go-sqlite3"。不幸的是,我在网络上没有找到其他解决方案,所以也许这里有人可以帮我解决这个问题?

非常感谢您的帮助!

英文:

I am working on a Bot. For this purposes I have decided to use the sqlite DB and GORM as ORM. Right now i am working on a simple "Connection" file, that should connect with sqlite db (which is in the same folder as the Conection.go) and migrate the predefined structs.

package database

import (
	_ "github.com/mattn/go-sqlite3"
	GlobalModels "goBot/modules/global/models"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"log"
	"os"
)

func Connect() *gorm.DB {

	log.Print(os.Getenv("DB_PATH"))

	db, err := gorm.Open(sqlite.Open("db.sqlite"), &gorm.Config{})
	if err != nil {
		log.Fatal(err)
	}

	return db
}

func Migrate() {

	db := Connect()

	errUser := db.AutoMigrate(GlobalModels.User{})
	if errUser != nil {
		log.Fatal(errUser)
	}

	errUserFlags := db.AutoMigrate(GlobalModels.UserFlags{})
	if errUserFlags != nil {
		log.Fatal(errUserFlags)
	}
}

And here are the predefined structs User.go and UserFlags.go:

package models

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Name   string `gorm:"column:name"`
	UserID int    `gorm:"column:user_id"`
	ChatID string `gorm:"column:chat_id"`
}


package models

import "gorm.io/gorm"

type UserFlags struct {
	gorm.Model
	UserID           int
	LastModuleChosen string `gorm:"column:last_module_chosen"`
	User             User   `gorm:"foreignKey:UserID"`
}

Here is the main function, where i try to migrate this models:

func main() {

	database.Migrate()

	fmt.Println("Starting the bot...")
	startBot()
}

When I run the main.go, the following 2 errors are occuring:
....\go\pkg\mod\gorm.io\driver\sqlite@v1.5.0\error_translator.go:9:35: undefined: sqlite3.ErrNoExtended
....\go\pkg\mod\gorm.io\driver\sqlite@v1.5.0\error_translator.go:14:36: undefined: sqlite3.Error

I have ensured, that the sqlite3 is set as env. variable in PATH and I have also installed the sqlite3 golang driver "github.com/mattn/go-sqlite3". Sadly I have not found any other solutions in the net, so maybe someone here could help me with this matter?

Thank you very much in advance!

答案1

得分: 3

这似乎是当前版本的GORM sqlite驱动程序(v1.5.0)的问题。https://github.com/go-gorm/sqlite/issues/142

看起来要么降级到1.4.4,要么等待新版本发布。

英文:

This appears to be an issue with the current version of the GORM sqlite driver (v 1.5.0)
https://github.com/go-gorm/sqlite/issues/142

Seems like it's necessary to either downgrade to 1.4.4 or wait for a new release.

huangapple
  • 本文由 发表于 2023年4月24日 18:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76091188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定