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

[Bug]: 使用属性关联功能,两个模型的属性值自动建立关系,更新关系时违反1-N约束导致失败 #485

Open
OpenAndrus opened this issue Apr 18, 2024 · 0 comments
Assignees

Comments

@OpenAndrus
Copy link

OpenAndrus commented Apr 18, 2024

Contact Details

No response

This bug is related to UI or API?

API

What happened?

Feat: 新增属性关联功能,支持通过两个模型的属性值自动建立关系

故障场景

例如,虚拟机A——运行于——计算节点1
后因故障虚拟机迁移,关系变为虚拟机A——运行于——计算节点2
更新虚拟机中(所在计算节点字段,将原值‘计算节点1’,修改为'计算节点2'),cmdb-api后台会自动尝试新增一条‘虚拟机A——运行于——计算节点2’的关系,导致与原有关系冲突,违反1-N约束。
前台并不会提示错误,能正常修改,虚拟机资产的字段值为运行于计算节点2,但是关系因更新失败会继续保持为运行于计算节点1

分析

这个逻辑在新版支持属性关联功能自动建立关系之前,我们自行同步信息的脚本是调用API,先查询现有关系,因为是1-N约束,存在已有关系需要先删除,然后再新增关系即可处理这种迁移的场景。

Version

newest

What browsers are you seeing the problem on?

No response

Relevant log output

query ci ids is: 0.001985311508178711
[2024-04-19 03:45:22,893: WARNING/ForkPoolWorker-8] [2024-04-19 03:45:22,893] INFO in cmdb: 17787 flush..........
[2024-04-19 03:45:22,893: INFO/ForkPoolWorker-8] 17787 flush..........
[2024-04-19 03:45:22,918: ERROR/ForkPoolWorker-8] Task cmdb.ci_cache[4d5615dd-1375-476c-8d11-d778e4a2fa30] raised unexpected: <BadRequest '400: Bad Request'>
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 477, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 760, in __protected_call__
    return self.run(*args, **kwargs)
  File "/data/apps/cmdb/api/lib/decorator.py", line 112, in wrapper
    return func(*args, **kwargs)
  File "/data/apps/cmdb/api/lib/decorator.py", line 96, in wrapper
    raise e
  File "/data/apps/cmdb/api/lib/decorator.py", line 85, in wrapper
    return func(*args, **kwargs)
  File "/data/apps/cmdb/api/tasks/cmdb.py", line 53, in ci_cache
    ci_dict and CIRelationManager.build_by_attribute(ci_dict)
  File "/data/apps/cmdb/api/lib/cmdb/ci.py", line 1114, in build_by_attribute
    CIRelationManager.add(parent.ci_id, ci_dict['_id'], valid=False)
  File "/data/apps/cmdb/api/lib/cmdb/ci.py", line 1002, in add
    cls._check_constraint(first_ci_id, first_ci.type_id, second_ci_id, second_ci.type_id, type_relation)
  File "/data/apps/cmdb/api/lib/cmdb/ci.py", line 960, in _check_constraint
    return abort(400, ErrFormat.relation_constraint.format("1-N"))
  File "/usr/local/lib/python3.8/site-packages/flask/helpers.py", line 277, in abort
    current_app.aborter(code, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/werkzeug/exceptions.py", line 861, in __call__
    raise self.mapping[code](*args, **kwargs)
werkzeug.exceptions.BadRequest: 400 Bad Request: Relationship constraint: 1-N, verification failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants