I am using gorm, and I have a nullable column gender
which can be set either male
or female
, but I am not able to set it back to NULL
using nil
once the column is set with a value of either male
or female
The following is my code. To simplify things a bit, I only include the fields related to my question
type User struct {
gorm.Model
Username string `gorm:"type:varchar(40);unique" json:"username"`
NickName string `gorm:"type:varchar(32)" json:"nickname"`
Gender *string `gorm:"type:enum('male', 'female');default:null" json:"gender"`
}
and then I update my gender to male:
db.Model(&User{}).Where(&User{
Model: gorm.Model{ID: 1},
}).Update(
&User{
Gender: utils.GetStringPtr("female"),
NickName: "nick name",
},
)
and then I tried to update the gender to NULL
db.Model(&User{}).Where(&User{
Model: gorm.Model{ID: 1},
}).Update(
&User{
Gender: nil,
NickName: "nick name",
},
)
However, the gender stays the same female
, and it is NOT changed to NULL
.
Any ideas how I can fix this?
null
? or you can`t set null? – ttrasn