gpt4 book ai didi

GORM FirstOrCreate 从不更新 bool 字段

转载 作者:行者123 更新时间:2023-12-01 22:21:02 26 4
gpt4 key购买 nike

在我的模型中,我有这样的字段:

HidePricingFromProfile     bool    `json:"hidePricingFromProfile"`
当我在数据库中更新模型时:
store.storage.DB.
Where("artist_account_id = ?", m.ArtistAccountID).
Assign(model).
FirstOrCreate(m).
Error
它仅将 bool 字段更新为真值。
该字段在 DB 中定义如下:
hide_pricing_from_profile boolean default false not null

最佳答案

来自 doc

NOTE all fields having a zero value, like 0, '', false or other zerovalues, won’t be saved into the database but will use its defaultvalueThe zero value of bool is false that's why it not updated. You need to use pointer of bool which zero value is nil then false value will be updated in the database.

HidePricingFromProfile     *bool    `json:"hidePricingFromProfile"`

关于GORM FirstOrCreate 从不更新 bool 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63578781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com