1

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 Answers1

3

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.