登录
首页 >  Golang >  Go问答

如何在Go中上传非空的0值?

来源:stackoverflow

时间:2024-03-16 21:09:31 310浏览 收藏

在 Go 中将非空 0 值上传到 psql 数据库时,可能会遇到“null value in column \"alarm\" violates not-null constraint”错误。这是因为 Go 无法将 0 识别为非空值。解决方法有两种: 1. 使用指针:将 `alarm` 声明为指针类型,并在初始化时将其设置为零值,如 `alarm: new(int)`。 2. 使用 `sql.nullint32`:使用 `sql.nullint32` 类型,并在扫描数据库值时将其转换为 `int`。

问题内容

我正在尝试将数据上传到 psql 数据库并收到以下消息:

error:
null value in column \"alarm\" violates not-null constraint

我发现警报值是 0,不是 null,而是 0...但是 golang 无法识别这一点...有没有办法解决这个问题?

type hsdatum struct {
    id int64
    uuid uuid.uuid
    hsassociationid int64
    timestamp time.time `sql:",notnull"`
    value int `sql:",notnull"`
    alarm int `sql:",notnull"`
    createdat time.time
    updatedat time.time
}


hsdatum := hsdata.hsdatum{
    hsassociationid: systemassoc.id,
    timestamp: starttime,
    value: 1,
    alarm: int(alarm), // 0 in this example
}

添加了有关数据库逻辑的更多信息:

func (h *HsDatum) BeforeInsert(db orm.DB) error {
    timeNow := timeutil.TimeNow()
    h.CreatedAt = timeNow
    h.UpdatedAt = timeNow
    // Manage distinct alarm table by checking for new value
    // Note: HS data needs to arrive in chronological order by timestamp
    needsUpdate := false
    lastAlarm, err := hsdistinctalarms.GetHsDistinctAlarmByMostRecent(h.HsAssociationID)
    var timeGap time.Duration
    if database.IsEmptyTable(err) {
        // No values so mark it
        needsUpdate = true
    } else if err == nil {
        if lastAlarm.Alarm != h.Alarm {
            // New value so mark it
            needsUpdate = true
        }
        timeGap = h.Timestamp.Sub(lastAlarm.LastUpdated)
    } else {
        logrus.Errorf("Failed to get most recent distinct alarm: %s", err)
    }
    if needsUpdate {
        // Create new alarm
        newAlarm := hsdistinctalarms.HsDistinctAlarm{
            HsAssociationID: h.HsAssociationID,
            Timestamp:       h.Timestamp,
            Alarm:           h.Alarm,
            LastUpdated:     h.Timestamp,
        }
        err = hsdistinctalarms.CreateHsDistinctAlarm(&newAlarm)
        if err != nil {
            logrus.Errorf("Failed to create hs distinct alarm: %s", err)
        }
    } else {
        // Update timestamp
        lastAlarm.LastUpdated = h.Timestamp
        err = hsdistinctalarms.UpdateHsDistinctAlarm(&lastAlarm)
        if err != nil {
            logrus.Errorf("Failed to update hs distinct alarm: %s", err)
        }
    }
    return nil
}

// CreateHsDatum creates the resource.
// It modifies the parameter in place and returns error information.
func CreateHsDatum(h *HsDatum) error {
    return database.GetDB().Insert(h)
}

解决方案


选项 1:
使用指针
例如

alarm *int `sql:",notnull"`

...

alarm: new(int) // init with zero value
or 
alarm: &alarm   // set a pointer to the alarm variable (should be int)

丑吗?是的。

选项 2:
使用 sql.nullint32
例如

Alarm sql.NullInt32 `sql:",notnull"`

...
var a sql.NullInt32
if err := a.Scan(alarm); err != nil {
  // handle error
}
...
Alarm: a
...

好了,本文到此结束,带大家了解了《如何在Go中上传非空的0值?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>