QOR示例中的panic

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

QOR example panic

问题

我正在尝试运行这个程序链接。但是当我运行它时,gorm 部分出现了一个 panic 错误。由于我对 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() {
	// 迁移 AuthIdentity 模型,AuthIdentity 用于保存认证信息,如用户名/密码、oauth 令牌,你可以更改它。
	gormDB.AutoMigrate(&auth_identity.AuthIdentity{})

	// 注册 Auth 提供者
	// 允许使用用户名/密码
	Auth.RegisterProvider(password.New(&password.Config{}))
}

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

	// 将 Auth 挂载到路由器
	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 结构体中出现了一个指针错误(我不知道如何更改它)。顺便说一下,我使用的是 go version go1.16.5 linux/amd64

英文:

I'm trying to run the program this link. However I run it and it leads to a panic on the gorm side. Since I'm new to go language, I have no idea how to debug it.

An mini-version of the program (without the fb, twitter and other login interfaces)

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))
}

I put the file which I name main.go in a folder (main.go is the only file in the folder) and then I run go mod init project_name && go mod tidy to initialize the project and install the needed packages. Then I do go run . and I get the following:

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

I'm really lost because I have no idea how to debug that. Seems a pointer in the auth_identity.AuthIdentity struc though (which I wouldn't know how to change). BTW, I'm using go version go1.16.5 linux/amd64.

答案1

得分: 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{})
英文:

This does not appear to be how to correctly open an SQLite database in Gorm.

You're missing the import of the SQLite driver, and instead of passing the string "sqlite3", you should be passing sqlite.Open("sample.db") and a pointer to a gorm.Config.

See 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{})

答案2

得分: 0

func init在建立数据库连接之前被执行,gorm无法进行迁移并在此处引发panic。

尝试使用以下代码:

func main(){
    gormDB, err = gorm.Open("sqlite3", "sample.db")
    if err != nil {
      log.Falal(err) // 如果无法打开数据库,则引发错误
    }
    // 数据库连接已建立,准备执行迁移:

    Auth = auth.New(&auth.Config{
        DB: gormDB,
    })

    // 迁移 AuthIdentity 模型,AuthIdentity 用于保存身份验证信息,如用户名/密码、OAuth 令牌,您可以更改它。
    err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})
    if err != nil {  
        log.Fatal(err) // 如果迁移失败,请不要忘记抛出异常
    }

    // 注册 Auth 提供程序
    // 允许使用用户名/密码
    Auth.RegisterProvider(password.New(&password.Config{}))

    err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})
    if err != nil {  
       log.Fatal(err) // 如果迁移失败,请不要忘记抛出异常
    }
    // 注册 Auth 提供程序
    // 允许使用用户名/密码
    Auth.RegisterProvider(password.New(&password.Config{}))

    mux := http.NewServeMux()

    // 将 Auth 挂载到路由器
    mux.Handle("/auth/", Auth.NewServeMux())
    http.ListenAndServe(":9000", manager.SessionManager.Middleware(mux))
}
英文:

func init is being executed before database connection is established, gorm fails to migrate and panic is thrown here.

try this code



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

得分: 0

问题是sqlite不被默认支持。在教程中,他们忘记在导入中添加以下行:

_ "github.com/jinzhu/gorm/dialects/sqlite"
英文:

The problem was that sqlite was not supported out of the box. In the tutorial they forgot to add in the imports the following line:

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

huangapple
  • 本文由 发表于 2021年7月13日 00:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/68351194.html
匿名

发表评论

匿名网友

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

确定