我將我的資料庫初始化為結構
type DBStorage struct {
db *gorm.DB
}
和
db, err := gorm.Open("postgres", DatabaseURL)
...
return &DBStorage{
db: db,
}
一切正常:查詢、更新和所有其他操作。但是后來我嘗試將背景關系添加到我的專案中,但它并沒有像那樣作業:
func (dbStorage DBStorage) PutOrder(order service.Order, ctx context.Context) error {
...
dbStorage.db.WithContext(ctx).Create(&order)
...
}
它說 WithContext 是一個未解決的參考。雖然dbStorage.db.Create(&order)作業正常。我應該如何解決這個問題?
我嘗試了一些愚蠢的事情,比如從結構中洗掉 *,但這有點打破了整個封裝的想法。還嘗試閱讀https://gorm.io/docs/method_chaining.html但不知道如何實作它以及它是否適合我的情況。如果是,我要求澄清一下。
uj5u.com熱心網友回復:
檢查您的匯入宣告。它應該import gorm.io/gorm代替import github.com/jinzhu/gorm.
庫的第一個版本是github.com/jinzhu/gorm型別gorm.DB沒有WithContext()方法的地方。
GORM V2 移至https://github.com/go-gorm/gorm并具有匯入路徑gorm.io/gorm。版本 2 添加了該DB.WithContext()方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524287.html
標籤:去戈戈姆去上下文
