1

I am and application where I am trying to indicate if a client is active or not. If I manually set the ClientActive boolean to False, I can successfully convert it to True. But if it is True, it will not set to False. I am using GoFiber and Gorm

models/Client.go

type Client struct {
gorm.Model
Slug           string `json:"slug" gorm:"unique"`
ClientName     string `json:"client_name"`
Address        string `json:"address,omitempty"`
Address2       string `json:"address_2,omitempty" gorm:"null"`
Phone          string `json:"phone" gorm:"null"`
PrimaryEmail   string `json:"primary_email" gorm:"null"`
SecondaryEmail string `json:"secondary_email" gorm:"null"`
ClientActive bool `json:"client_active" gorm:"default:true"`
Contacts     []Contact
Devices      []Device
}

handlers/clientHandler.go

func ClientUpdate(c \*fiber.Ctx) error {
slug := c.Params("slug")

    var data models.Client
    
    err := c.BodyParser(&data)
    if err != nil {
        return err
    }
    
    // todo: the ClientActive variable will set as true, but never false
    client := &models.Client{
        ClientName:     data.ClientName,
        Address:        data.Address,
        Address2:       data.Address2,
        Phone:          data.Phone,
        PrimaryEmail:   data.PrimaryEmail,
        SecondaryEmail: data.SecondaryEmail,
        ClientActive:   data.ClientActive,
    }
    
    err = database.DB.Model(&data).Where("slug = ?", slug).Updates(&client).Error
    if err != nil {
        return err
    }
    
    return c.JSON(client)

}

All other lines update without issue. The only problem is the ClientActive boolean.

The full code is available at https://github.com/simpleittools/assetAPI

I have confirmed that the data is being sent as a boolean. I have run a fmt.Prtintln(client) both before and after the database input and it does show correctly as false.

I am getting no errors in the process.

RyanAK
  • 13
  • 3
  • by default the value is true. If I go into the database, and manually change the value to false, it will identify correctly on a GET request. If I send a PATCH where I set ClientActive to True, the column updates as expected. But running a PATCH request where ClientActive = False, the column will remain True. All other columns update as expected. I tried by setting the Default to False, and then ran though the process of running a PATCH request to True. It will change to True. But then running again to set as False, results in no change. So basically, once it is True, it stays True. – RyanAK Apr 26 '23 at 03:23
  • @CeriseLimón, thank you. This is resolved. Please post your comment as an answer, so I can mark it as the correct solution. Simply a pointer to the bool type in the model. – RyanAK Apr 26 '23 at 22:31

1 Answers1

0

The GORM default values documentation says:

Any zero value like 0, '', false won’t be saved into the database for those fields defined default value, you might want to use pointer type or Scanner/Valuer to avoid this

Fix by changing the field type to *bool:

type Client struct {
    ⋮
    ClientActive *bool `json:"client_active" gorm:"default:true"`
    ⋮
}
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242