GORM Playground Link

Description

Here is the test code


CREATE TABLE `test_table` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` char(32) COLLATE utf8mb4_bin DEFAULT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;


package main

import (
    "fmt"
    "log"

    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
    "gorm.io/gorm/schema"
)

type TestTable struct {
    ID     int    `json:"id"`
    Name   string `json:"name"`
    Status int    `json:"status"`
}

func ConnectDB() *gorm.DB {
    dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4", "test", "test", "localhost", 3306, "test_db")
    gormConfig := &gorm.Config{
        NamingStrategy: &schema.NamingStrategy{SingularTable: true},
        Logger:         logger.Default.LogMode(logger.Info),
    }
    db, err := gorm.Open(mysql.Open(dsn), gormConfig)
    if err != nil {
        log.Fatal(err)
    }
    log.Println("database connected!")
    return db
}

func main() {
    db := ConnectDB()
    var tb TestTable
    db.Where("id = ?", 1).First(&tb)
    db.Model(&tb).Updates(TestTable{Name: "status updated", Status: 1})
    var tb1 TestTable
    db.Where("id = ?", 1).First(&tb1)
        // if Status is set to 0, the generated sql will not include status field, but if not 0, it will work
    db.Model(&tb1).Updates(TestTable{Name: "status will not updated", Status: 0})
}

WX20240303-111059@2x

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: ivila

@panyox I think this work as expected, a "zero value" is skipped during update, if you do want to update with zero value of int, I suggest you should use a map[string]interface{} as parameter instead, also, you can check the database/sql.NullInt64.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: panyox

@panyox I think this work as expected, a "zero value" is skipped during update, if you do want to update with zero value of int, I suggest you should use a map[string]interface{} as parameter instead, also, you can check the database/sql.NullInt64.

em..., ok, that's a little confused, but map is worked.