如何使用Gorm在Golang中进行部分更新对象?

发布于 2025-02-08 10:51:46 字数 1908 浏览 1 评论 0原文

我有以下结构:

type Shop struct {
    ID          uint32    `gorm:"primary_key;auto_increment" json:"id"`
    Name        string    `gorm:"size:255;not null;unique" json:"name"`
    Email       string    `gorm:"size:100;not null;unique" json:"email"`
    PhoneNumber string    `gorm:"size:50;unique" json:"phone_number"`
    Password    string    `gorm:"size:100;not null;" json:"password"`
    CreatedAt   time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
    UpdatedAt   time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}

我的结构具有以下功能,以便更新数据库中的对象;当每个字段传递时,这都是正常工作的:

func (shop *Shop) UpdateAShop(db *gorm.DB, uid uint32) (*Shop, error) {

    // To hash the password
    err := shop.BeforeSave()
    if err != nil {
        log.Fatal(err)
    }
    db = db.Debug().Model(&Shop{}).Where("id = ?", uid).Take(&Shop{}).Updates(
        map[string]interface{}{
            "password":     shop.Password,
            "name":         shop.Name,
            "phone_number": shop.PhoneNumber,
            "email":        shop.Email,
            "update_at":    time.Now(),
        },
    )
    if db.Error != nil {
        print("database error")
        return &Shop{}, db.Error
    }
    // This is the display the updated shop
    err = db.Debug().Model(&Shop{}).Where("id = ?", uid).Take(&shop).Error
    if err != nil {
        return &Shop{}, err
    }
    return shop, nil
}

但是,如果我想使用补丁http请求进行部分更新,则它不起作用并导致以下错误:

{
    "error": "Incorrect Details"
}

我要通过的数据是类似的这是:

{
    "name": "My Shop"
}

我已经浏览了GORM文档,但找不到有关部分更新的信息(也许我没有查看文档的正确部分),我已经通过Internet进行了搜索,也找不到简洁的东西。我将如何做到?

I have the following struct:

type Shop struct {
    ID          uint32    `gorm:"primary_key;auto_increment" json:"id"`
    Name        string    `gorm:"size:255;not null;unique" json:"name"`
    Email       string    `gorm:"size:100;not null;unique" json:"email"`
    PhoneNumber string    `gorm:"size:50;unique" json:"phone_number"`
    Password    string    `gorm:"size:100;not null;" json:"password"`
    CreatedAt   time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
    UpdatedAt   time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}

And I am having the following function for my struct in order to update the object in the database; which is working fine when every field is passed:

func (shop *Shop) UpdateAShop(db *gorm.DB, uid uint32) (*Shop, error) {

    // To hash the password
    err := shop.BeforeSave()
    if err != nil {
        log.Fatal(err)
    }
    db = db.Debug().Model(&Shop{}).Where("id = ?", uid).Take(&Shop{}).Updates(
        map[string]interface{}{
            "password":     shop.Password,
            "name":         shop.Name,
            "phone_number": shop.PhoneNumber,
            "email":        shop.Email,
            "update_at":    time.Now(),
        },
    )
    if db.Error != nil {
        print("database error")
        return &Shop{}, db.Error
    }
    // This is the display the updated shop
    err = db.Debug().Model(&Shop{}).Where("id = ?", uid).Take(&shop).Error
    if err != nil {
        return &Shop{}, err
    }
    return shop, nil
}

But, if I want to do a partial update using a PATCH HTTP request, it doesn't work and result in the following error:

{
    "error": "Incorrect Details"
}

The data that I'm trying to pass is something like this:

{
    "name": "My Shop"
}

I have gone through GORM documentation and I couldn't find something about partial updates (Maybe I am not looking at the right part of the documentation) and I have searched through the internet and did not found something concise. How would I be able to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文