go-gorm / go-gorm/datatypes

Question(JSONB Postgres): Update jsonb key-value through GORM v2

Open
#7 0 comments 0 reactions 1 assignee Claimed by @jinzhu View on GitHub
Dominant language
Go
Stars
831
Forks
124
PR merge metrics
No merged PRs in 30d

Description

I am using GORM v2. I have a datatypes.JSON field in struct. I want to do a partial update on a key in jsonb field using GORM updates. Right now i pass map to GORM updates method to update the fields. But if i pass jsonb field, it gets replaced with new data. How can I partially update jsonb field?
```
type test struct {
name string
age int
address datatypes.JSON
}
```
I am doing update using the following, where 'profile' is a map[string]interface{}.
```
db.Where("name = ?", "abc").Updates(profile).Error()
```
Assuming address has pincode, city, country field populated. Suppose I want to just update the city field in the address JSONB, I build profile(map[string]interface{}) like shown below
```
address: {
"city": "NewCity"
}
```
It replaces the entire address field with the above data rather than just updating the city.
I need to be able to update only city field in json; pincode and country should remain the same. How can I do that?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.