go-gorm/datatypes

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

goakshit opened this issue · 0 comments

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?