如何使用Gorm在Golang中进行部分更新对象?
我有以下结构:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论