QOR example panic

198 Views Asked by At

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/[email protected]/dialect_sqlite3.go:81 +0xd8
github.com/jinzhu/gorm.(*Scope).autoMigrate(0xc000212e00, 0x85d340)
        /home/username/go/pkg/mod/github.com/jinzhu/[email protected]/scope.go:1268 +0xbb
github.com/jinzhu/gorm.(*DB).AutoMigrate(0xc00033de10, 0xc00028de18, 0x1, 0x1, 0xc00039b9c0)
        /home/username/go/pkg/mod/github.com/jinzhu/[email protected]/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.

3

There are 3 best solutions below

0
silgon On BEST ANSWER

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"
0
vodolaz095 On

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

}

1
user229044 On

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