【问题标题】:QOR example panicQOR 示例恐慌
【发布时间】:2021-09-21 20:18:17
【问题描述】:

我正在尝试运行程序this link。但是我运行它并导致gorm 方面的恐慌。由于我是 go 语言的新手,我不知道如何调试它。

程序的迷你版(没有fb、twitter等登录界面)

package main
import (
    "net/http"
    "github.com/qor/auth"
    "github.com/qor/auth/auth_identity"
    "github.com/qor/auth/providers/password"
    "github.com/qor/session/manager"
    "github.com/jinzhu/gorm"
)

var (
    gormDB, _ = gorm.Open("sqlite3", "sample.db")
    Auth = auth.New(&auth.Config{
        DB: gormDB,
    })
)

func init() {
    // Migrate AuthIdentity model, AuthIdentity will be used to save auth info, like username/password, oauth token, you could change that.
    gormDB.AutoMigrate(&auth_identity.AuthIdentity{})

    // Register Auth providers
    // Allow use username/password
    Auth.RegisterProvider(password.New(&password.Config{}))
}

func main() {
    mux := http.NewServeMux()

    // Mount Auth to Router
    mux.Handle("/auth/", Auth.NewServeMux())
    http.ListenAndServe(":9000", manager.SessionManager.Middleware(mux))
}

我将我命名为main.go 的文件放在一个文件夹中(main.go 是该文件夹中唯一的文件),然后我运行go mod init project_name && go mod tidy 来初始化项目并安装所需的包。然后我执行go run . 并得到以下信息:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x6d0441]

goroutine 1 [running]:
database/sql.(*DB).conn(0x0, 0x953c10, 0xc000016150, 0x1, 0x7f9cd7c2b108, 0x18, 0xc00028dac8)
        /snap/go/7736/src/database/sql/sql.go:1197 +0x41
database/sql.(*DB).query(0x0, 0x953c10, 0xc000016150, 0x8db986, 0x40, 0xc00039be10, 0x1, 0x1, 0xc000109b01, 0xc00028dba0, ...)
        /snap/go/7736/src/database/sql/sql.go:1623 +0x66
database/sql.(*DB).QueryContext(0x0, 0x953c10, 0xc000016150, 0x8db986, 0x40, 0xc00039be10, 0x1, 0x1, 0xc00028dc10, 0x40cefb, ...)
        /snap/go/7736/src/database/sql/sql.go:1605 +0xd4
database/sql.(*DB).QueryRowContext(...)
        /snap/go/7736/src/database/sql/sql.go:1706
database/sql.(*DB).QueryRow(0x0, 0x8db986, 0x40, 0xc00039be10, 0x1, 0x1, 0xf)
        /snap/go/7736/src/database/sql/sql.go:1717 +0x8b
github.com/jinzhu/gorm.sqlite3.HasTable(0x953d60, 0x0, 0xc00038ebf0, 0xc00038ebf0, 0xf, 0x11)
        /home/username/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/dialect_sqlite3.go:81 +0xd8
github.com/jinzhu/gorm.(*Scope).autoMigrate(0xc000212e00, 0x85d340)
        /home/username/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/scope.go:1268 +0xbb
github.com/jinzhu/gorm.(*DB).AutoMigrate(0xc00033de10, 0xc00028de18, 0x1, 0x1, 0xc00039b9c0)
        /home/username/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/main.go:689 +0x97
main.init.0()
        /home/username/tmp/qor-test/main.go:20 +0x7b
exit status 2

我真的迷路了,因为我不知道如何调试它。似乎是auth_identity.AuthIdentity struc 中的一个指针(我不知道如何更改)。顺便说一句,我正在使用go version go1.16.5 linux/amd64

【问题讨论】:

    标签: go qor


    【解决方案1】:

    这似乎不是在 Gorm 中正确打开 SQLite 数据库的方法。

    您缺少 SQLite 驱动程序的导入,而不是传递字符串“sqlite3”,您应该传递 sqlite.Open("sample.db") 和指向 gorm.Config 的指针。

    https://gorm.io/docs/connecting_to_the_database.html#SQLite

    import (
      "gorm.io/driver/sqlite"
      "gorm.io/gorm"
    )
    
    // github.com/mattn/go-sqlite3
    db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
    

    【讨论】:

    • 您的回答让我找到了很好的线索。我需要在导入中添加_ "github.com/jinzhu/gorm/dialects/sqlite"。我试过你的代码,它对我不起作用。但是我看到了qor 包的另一个示例,其中包含我刚刚发送给您的代码。谢谢!
    【解决方案2】:

    func init在建立数据库连接之前执行,gorm迁移失败,这里抛出panic。

    试试这个代码

    
    
    func main(){
        gormDB, err = gorm.Open("sqlite3", "sample.db")
        if err != nil {
          log.Falal(err) // thrown, if database cannot be opened
        }
        // database connection is established, ready to perform migrations:
    
    
        Auth = auth.New(&auth.Config{
            DB: gormDB,
        })
    
        // Migrate AuthIdentity model, AuthIdentity will be used to save auth info, like username/password, oauth token, you could change that.
        err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})
        if err != nil {  
            log.Fatal(err) // do not forget to throw exception, if migration fails
        }
    
        // Register Auth providers
        // Allow use username/password
        Auth.RegisterProvider(password.New(&password.Config{}))
    
    
        err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})
        if err != nil {  
           log.Fatal(err) // do not forget to throw exception, if migration fails
        }
        // Register Auth providers
        // Allow use username/password
        Auth.RegisterProvider(password.New(&password.Config{}))
    
        mux := http.NewServeMux()
    
        // Mount Auth to Router
        mux.Handle("/auth/", Auth.NewServeMux())
        http.ListenAndServe(":9000", manager.SessionManager.Middleware(mux))
    
    }
    
    

    【讨论】:

      【解决方案3】:

      问题在于 sqlite 不支持开箱即用。在教程中,他们忘记在导入中添加以下行:

      _ "github.com/jinzhu/gorm/dialects/sqlite"
      

      【讨论】:

        猜你喜欢
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 2013-11-21
        • 2013-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多