添加新的alert时,为什么需要把alert.id设置为0呢
sunism opened this issue · 5 comments
添加新的alert时,为什么需要把alert.id设置为0呢, alert.id为主键,每一个新的alert组件都设置为0之后,不就覆盖了以前的alert了吗?
alert.Id = 0 //reset the "Id" to 0,which is very important:after a record is inserted,the value of "Id" will not be 0,but the auto primary key of the record
这个解释不是很明白?
添加新的alert时,为什么需要把alert.id设置为0呢, alert.id为主键,每一个新的alert组件都设置为0之后,不就覆盖了以前的alert了吗?
alert.Id = 0 //reset the "Id" to 0,which is very important:after a record is inserted,the value of "Id" will not be 0,but the auto primary key of the record
这个解释不是很明白?
因为alert这个用于插入报警的结构体是复用的(每次插入一个新报警都用的该结构体实例),而执行插入后alert.Id的值会变成新插入的记录的id,因此对于下一条报警,如果不把alert.Id重置为0,则执行插入时alert.Id的值就是前一条报警的主键id值,会导致主键冲突
// insert an new alert
var alert Alerts
alert.Id = 0 //reset the "Id" to 0,which is very important:after a record is inserted,the value of "Id" will not be 0,but the auto primary key of the record
alert.Rule = &Rules{Id: a.ruleId}
alert.Labels = a.label
alert.FiredAt = &a.firedAt
alert.Description = elemt.Annotations.Description
alert.Summary = elemt.Annotations.Summary
alert.Count = -1
alert.Value = elemt.Value
alert.Status = int8(elemt.State)
alert.Hostname = a.hostname
alert.ConfirmedAt = &todayZero
alert.ConfirmedBefore = &todayZero
alert.ResolvedAt = &todayZero
_, err = Ormer().Insert(&alert)
if err != nil {
logs.Error("Insert alter failed:%s", err)
}
这个alert是一个新的变量,请问跟结构体复用,有什么联系?
// insert an new alert var alert Alerts alert.Id = 0 //reset the "Id" to 0,which is very important:after a record is inserted,the value of "Id" will not be 0,but the auto primary key of the record alert.Rule = &Rules{Id: a.ruleId} alert.Labels = a.label alert.FiredAt = &a.firedAt alert.Description = elemt.Annotations.Description alert.Summary = elemt.Annotations.Summary alert.Count = -1 alert.Value = elemt.Value alert.Status = int8(elemt.State) alert.Hostname = a.hostname alert.ConfirmedAt = &todayZero alert.ConfirmedBefore = &todayZero alert.ResolvedAt = &todayZero _, err = Ormer().Insert(&alert) if err != nil { logs.Error("Insert alter failed:%s", err) }
这个alert是一个新的变量,请问跟结构体复用,有什么联系?
原来的代码对于一次收到的多个报警记录而言是共用一个Alerts结构体变量的,后来改了一下,但注释一直没有去掉,所以造成了误解,抱歉:P
请问,这个alert.Id = 0是不是就不需要了哦,id是自动增长的,在保存到数据库时就自动生成id,不需要再设置id=0呢
请问,这个alert.Id = 0是不是就不需要了哦,id是自动增长的,在保存到数据库时就自动生成id,不需要再设置id=0呢
对,可以自己试一下