3 回答

TA貢獻1864條經驗 獲得超6個贊
這似乎不是在戈爾姆中正確打開 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{})

TA貢獻1898條經驗 獲得超8個贊
func init在建立數據庫連接之前執行,gorm 無法遷移,并且在此處引發恐慌。
試試這個代碼
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))
}

TA貢獻1816條經驗 獲得超6個贊
問題是沒有開箱即用的支持。在教程中,他們忘記在導入中添加以下行:sqlite
_ "github.com/jinzhu/gorm/dialects/sqlite"
- 3 回答
- 0 關注
- 116 瀏覽
添加回答
舉報