79650876

Date: 2025-06-03 08:27:33
Score: 1.5
Natty:
Report link

The best way current may be using Gorm hook, though we have to list all columns we need to ignore insert/update. GORM Hooks

In my case, I want to ignore insert field A if it is empty and B if it equals 0:

func (c C) BeforeCreate(tx *gorm.DB) (err error) {
    if c.A == "" {
        tx.Statement.Omits = append(tx.Statement.Omits, "a_column")
    }
    if c.B == "" {
        tx.Statement.Omits = append(tx.Statement.Omits, "b_column")
    }
    return
}
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Thành An