Skip to content

Commit 2e85532

Browse files
author
breezeli
authored
Merge pull request #3619 from vlon/v3.2.x-instasst
fix: 修复模型关联n-n时,俩实例之间的关联可以重复创建的bug Former-commit-id: e543ed0
2 parents 7a33373 + f00aa87 commit 2e85532

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

resources/errors/common/default/topo_server.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"1101059": "bk_inst_name %s 值有重复",
8484
"1101060": "关联类型已经被使用",
8585
"1101061": "关联关系为1:n的,不能同时关联多个源模型实例",
86+
"1101062": "两实例间已经存在关联,不能重复创建",
8687
"1001060":"创建模型分组唯一失败",
8788
"1001061":"更新模型分组唯一失败",
8889
"1001062":"删除模型分组唯一失败",

resources/errors/english/en/topo_server.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"1101059": "bk_inst_name %s is duplicate.",
8484
"1101060": "association kind has already been used.",
8585
"1101061": "The relationship is 1:n, and multiple source model instances cannot be associated at the same time.",
86+
"1101062": "There is already an association between the two instances and cannot be created repeatedly",
8687
"1001060":"create model unique constrains failed",
8788
"1001061":"update model unique constrains failed",
8889
"1001062":"delete model unique constrains failed",

src/common/errInfo.go

+2
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ const (
310310
CCErrorTopoAssociationKindHasBeenUsed = 1101060
311311
// create new instance for a new association, but association map is 1:n
312312
CCErrorTopoCreateMultipleInstancesForOneToManyAssociation = 1101061
313+
// create instance association, but two instances are already associated
314+
CCErrorAssociationBetweenTwoInstacneAlreadyExist = 1101062
313315

314316
CCErrTopoAppDeleteFailed = 1001031
315317
CCErrTopoAppUpdateFailed = 1001032

src/scene_server/topo_server/core/operation/association.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,25 @@ func (a *association) CreateInst(params types.ContextParams, request *metadata.C
612612
return nil, err
613613
}
614614

615-
switch modelAsst.Mapping {
615+
// Check if two instances are already associated
616+
cond = condition.CreateCondition()
617+
cond.Field(common.BKInstIDField).Eq(request.InstId)
618+
cond.Field(common.BKAsstInstIDField).Eq(request.AsstInstId)
619+
cond.Field(common.AssociationObjAsstIDField).Eq(request.ObjectAsstId)
620+
instAsst, err := a.SearchInst(params, &metadata.SearchAssociationInstRequest{Condition: cond.ToMapStr()})
621+
if err != nil {
622+
blog.Errorf("create association instance, but check instance with cond[%v] failed, err: %v", cond, err)
623+
return nil, params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
624+
}
625+
if !instAsst.Result {
626+
blog.Errorf("create association instance, but check instance with cond[%v] failed, err: %s", cond, resp.ErrMsg)
627+
return nil, params.Err.New(resp.Code, resp.ErrMsg)
628+
}
629+
if len(instAsst.Data) >= 1 {
630+
return nil, params.Err.Error(common.CCErrorAssociationBetweenTwoInstacneAlreadyExist)
631+
}
616632

633+
switch modelAsst.Mapping {
617634
case metatype.OneToOneMapping:
618635
cond := condition.CreateCondition()
619636
cond.Field(common.AssociationObjAsstIDField).Eq(request.ObjectAsstId)

0 commit comments

Comments
 (0)