problem description:
the problem we encountered this morning has been going on all morning, but we still haven"t found out what the problem is. Come to ask
purpose: use gorm
to modify the Boolean value of a field in the database table
Code first:
func ChangeActive(id int) (*models.Comment, error) {
var (
model *models.Comment
pre_model models.PreComment
err error
)
//
err = common.DB.Where("id = ?", id).First(&model).Error
if err != nil {
return nil, err
}
//
model.IsActive = !model.IsActive
//
common.DB.Model(&model).Updates(&model)
if !model.IsActive {
pre_model.Create(common.DB)
}
return model, err
}
this function receives aThe Bug: Boolean value encountered byID
, queries out the data corresponding toID
in the database, and modifies theactive
field of this data to reverse it. Finally, update the database.
cannot be updated all the time.
troubleshooting process: at first I thought there was something wrong with the code statement, so I used goland"s Debug mode and found that there was nothing wrong with the code.
then I verify that modifies another field (model.CompanyID = 3)
before updating the data table to see if the modification is successful. The result is that the modification is successful, but the Boolean value remains the same.