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 关注
- 89 浏览
添加回答
举报