第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 Go 中增加結(jié)構(gòu)值

在 Go 中增加結(jié)構(gòu)值

Go
慕的地6264312 2022-07-11 16:43:35
我希望看到visits每個(gè)GET請(qǐng)求的增量,/foo但它仍然是1. 我在這里做錯(cuò)了什么?package mainimport (    "log"    "github.com/gofiber/fiber/v2"    "gorm.io/driver/sqlite"    "gorm.io/gorm")// Item modeltype Item struct {    gorm.Model    UID    int64  `gorm:"primaryKey;autoIncrement"`    Name   string `gorm:"index;not null"`    Visits int32  `gorm:"default 0"`}func main() {    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})    if err != nil {        panic(err)    }    db.AutoMigrate(&Item{})    db.Create(&Item{        Name: "foo",    })    app := fiber.New(fiber.Config{})    app.Get("/:name", func(c *fiber.Ctx) error {        var i Item        db.First(&i, "name = ?", c.Params("name"))        if i.Name == "" {            return c.Status(fiber.StatusNotFound).JSON(&fiber.Map{                "message": "Not found",            })        }        db.Model(&i).Update("visits", i.Visits+1)        return c.JSON(i)    })    log.Println("Listening...")    log.Fatal(app.Listen(":3000"))}
查看完整描述

1 回答

?
守候你守候我

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊

如果您記錄以下錯(cuò)誤:


if err := db.Model(&i).Update("visits", i.Visits+1).Error; err != nil {

    fmt.Printf("update err != nil; %v\n", err)

}

你會(huì)看到它說:“需要條件”。所以,你可以像這樣解決這個(gè)問題:


if err := db.Model(&i).Where("name = ?", i.Name).Update("visits", i.Visits+1).Error; err != nil {

    fmt.Printf("update err != nil; %v\n", err)

}

這是有關(guān)GORM 中的錯(cuò)誤處理的更多詳細(xì)信息


編輯:您的示例實(shí)際上存在一個(gè)更大的問題。問題是您將其定義UID為Item模型的一部分,這與提供的內(nèi)容沖突gorm.Model。您可以在聲明模型中看到以下模型定義:


// gorm.Model definition

type Model struct {

  ID        uint           `gorm:"primaryKey"`

  CreatedAt time.Time

  UpdatedAt time.Time

  DeletedAt gorm.DeletedAt `gorm:"index"`

}

當(dāng)添加到您的Item類型/模型中時(shí),您將獲得:


type Item struct {

  // gorm.Model

  ID        uint           `gorm:"primaryKey"`

  CreatedAt time.Time

  UpdatedAt time.Time

  DeletedAt gorm.DeletedAt `gorm:"index"`

  // your fields

  UID    int64  `gorm:"primaryKey;autoIncrement"`

  Name   string `gorm:"index;not null"`

  Visits int32  `gorm:"default 0"`

}

似乎這會(huì)導(dǎo)致您的數(shù)據(jù)庫(kù)表以奇怪的狀態(tài)創(chuàng)建。您可能會(huì)在返回的 JSON 有效負(fù)載中注意到IDANDUID都等于 0。當(dāng)您多次啟動(dòng)/停止服務(wù)器并查看創(chuàng)建的其他記錄(因?yàn)槟鷇b.Create()位于頂部)時(shí),您最終會(huì)得到多個(gè)項(xiàng)目名稱為“foo”,所有的ID和UID都為 0...這就是為什么 GORM 在沒有 WHERE 子句的情況下無法更新項(xiàng)目的原因,因?yàn)橹麈I沒有在表上正確設(shè)置。


如果您從模型中刪除 UID(或者甚至只是從中刪除“primaryKey”),則可以使用該Update()方法而無需 where 條件。因此,您的模型應(yīng)如下所示:


// Item model

type Item struct {

    gorm.Model

    Name   string `gorm:"index;not null"`

    Visits int32  `gorm:"default 0"`

}

更改模型/類型后,請(qǐng)確保刪除test.db文件,以便使用新/正確格式重新創(chuàng)建表。


最后,關(guān)于我的原始答案,您還應(yīng)該看到 GORM 自動(dòng)將錯(cuò)誤記錄到您的控制臺(tái),而無需像我建議的那樣專門處理它們。


查看完整回答
反對(duì) 回復(fù) 2022-07-11
  • 1 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)