英文:
Golang Boolean not detecting on PATCH
问题
我是一个应用程序,我正在尝试指示客户是否活动。如果我手动将ClientActive布尔值设置为False,我可以成功将其转换为True。但如果它是True,它将无法设置为False。我正在使用GoFiber和Gorm。
在models/Client.go文件中,Client结构体定义如下:
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文件中,ClientUpdate函数如下:
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)
}
除此之外的所有行都能正常更新,唯一的问题是ClientActive布尔值。
完整的代码可以在https://github.com/simpleittools/assetAPI上找到。
我已确认数据以布尔值的形式发送。我在数据库输入之前和之后都运行了fmt.Println(client),它显示为false。
在整个过程中,我没有收到任何错误。
英文:
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.
答案1
得分: 0
GORM默认值文档中提到:
> 任何零值,如0、''、false,对于那些定义了默认值的字段,都不会保存到数据库中,您可能希望使用指针类型或Scanner/Valuer来避免这种情况。
通过将字段类型更改为*bool
来修复:
type Client struct {
⋮
ClientActive *bool `json:"client_active" gorm:"default:true"`
⋮
}
英文:
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"`
⋮
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论