Go | Updating the database with null values while preserving the values in other columns

I am working on a REST API. At the moment I’ve been creating a PATCH endpoint to update user information such as their username, birthday, etc. I’ve been stuck for a couple days trying to make updating the information work with my Postgres database.

I am using Bun ORM https://bun.uptrace.dev/guide/query-update.html to interact with the database. The issues I’ve been having stem from this code snippet below:

/* Database Model */
type User struct {
    Id             uuid.UUID `json:"id,omitempty" bun:"column:pk,type:uuid,default:gen_random_uuid()"`
    Email          string    `json:"email,omitempty"`
    Name           string    `json:"name,omitempty"`
    Username       string    `json:"username,omitempty"`
    Password       string    `json:"-" dbKey:"password"`
    Verified       bool      `json:"verified,omitempty"`
    EmailVerified  bool      `json:"emailVerified,omitempty"`
    Avatar         *string   `json:"avatar,omitempty"`
    GraduationYear int       `json:"graduationYear,omitempty"`
    Degree         *string   `json:"degree,omitempty"`
    Major          *string   `json:"major,omitempty"`
    Intrests       int       `json:"intrests,omitempty"`
    Clubs          int       `json:"clubs,omitempty"`
    Sports         int       `json:"sports,omitempty"`
    NotificationId string    `json:"notificationId,omitempty"`
    CreatedAt      time.Time `json:"createdAt,omitempty" bun:"type:timestamp,default:now()"`
    UpdatedAt      time.Time `json:"updatedAt,omitempty" bun:"type:timestamp,default:now()"`
}

func (r *userRepositoryImpl) UpdateUser(userId string, data models.User) (*models.User, error) {
    user := models.User(data)

    _, err := r.db.NewUpdate().Model(&user).Where("id = ?", userId).Returning("*").Exec(r.context)
    if err != nil {
        return nil, err
    }

    return &user, nil
}

This function is meant to update the user depending on what data I give the User struct. However, if I want to update only ONE of the fields, it sets all of the columns in my Postgres database to their default or zero value. Example:

/* This resets all other column values to their default or zero value, just affecting the avatar column */
updatedUser, err := s.userRepository.UpdateUser(userId, models.User{
    Avatar: &avatarUrl,
})

I later came across this method in Bun ORM: Omit Zero Values. This allowed me to update the avatar column only, and the ORM ignored the empty fields in the struct. But then I cannot reset the avatar column back to an empty string, cause it will be ignored.

I have tried several things, most likely really over-engineered. I even tried reflection since I got desperate.

  1. Tried converting the User struct fields that weren’t empty to a map[string]interface{}, and passing that to the Model method. Unfortunately the ORM did not convert my types to how they were defined in my User struct since I used a map.
  2. I tried merging the old struct with a struct containing the data I wanted to update, which worked at first but was extremely hacky and I wouldn’t prefer to use that as a solution.
  3. I tried using pointer values like *string to represent null values, but just got the same result as I described at the beginning of my post.

I would really appreciate some guidance and help with this, part of me feels like I’m over thinking it, and I would like to hear others thoughts. If extra clarification is needed, Ill edit this post as necessary.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật