Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加新的alert时,为什么需要把alert.id设置为0呢 #94

Open
sunism opened this issue Feb 2, 2021 · 5 comments
Open

添加新的alert时,为什么需要把alert.id设置为0呢 #94

sunism opened this issue Feb 2, 2021 · 5 comments

Comments

@sunism
Copy link

sunism commented Feb 2, 2021

添加新的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
这个解释不是很明白?

@jayryu
Copy link
Collaborator

jayryu commented Feb 2, 2021

添加新的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值,会导致主键冲突

@sunism
Copy link
Author

sunism commented Feb 3, 2021

                       // 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是一个新的变量,请问跟结构体复用,有什么联系?

@jayryu
Copy link
Collaborator

jayryu commented Feb 3, 2021

                       // 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

@sunism
Copy link
Author

sunism commented Feb 4, 2021

请问,这个alert.Id = 0是不是就不需要了哦,id是自动增长的,在保存到数据库时就自动生成id,不需要再设置id=0呢

@jayryu
Copy link
Collaborator

jayryu commented Feb 4, 2021

请问,这个alert.Id = 0是不是就不需要了哦,id是自动增长的,在保存到数据库时就自动生成id,不需要再设置id=0呢

对,可以自己试一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants