Skip to content

fix: delete TopologyServiceApplication links before TopologyService rows (fixes #5439)#6171

Open
chengyixu wants to merge 1 commit intokeephq:mainfrom
chengyixu:fix/topology-service-application-fk-deletion
Open

fix: delete TopologyServiceApplication links before TopologyService rows (fixes #5439)#6171
chengyixu wants to merge 1 commit intokeephq:mainfrom
chengyixu:fix/topology-service-application-fk-deletion

Conversation

@chengyixu
Copy link
Copy Markdown

Summary

Fixes a ForeignKeyViolation that crashes process_topology whenever a provider's topology services belong to a TopologyApplication.

Root cause

process_topology refreshes topology data by deleting then re-inserting all TopologyService rows for a given provider. The deletion order was:

  1. Delete TopologyServiceDependency (correct — has CASCADE)
  2. Delete TopologyService (fails when a row is still referenced by TopologyServiceApplication.service_id)

The TopologyServiceApplication join table does not have ON DELETE CASCADE, so step 2 raised:

sqlalchemy.exc.IntegrityError: ForeignKeyViolation:
update or delete on table "topologyservice" violates
foreign key constraint "topologyserviceapplication_service_id_fkey"

This is the exact traceback reported in #5439.

Fix

Delete the TopologyServiceApplication rows for the affected services before deleting the TopologyService rows, using the same filter pattern already used for TopologyServiceDependency.

Changes

  • keep/api/tasks/process_topology_task.py — add TopologyServiceApplication deletion step
  • tests/test_topology.py — regression test that would have caught this (and confirms the fix)

Closes #5439

When process_topology refreshes provider topology data it deletes all
existing TopologyService rows for that provider. If any of those services
were associated with a TopologyApplication (via the TopologyServiceApplication
join table), the delete violated the foreign-key constraint on
topologyserviceapplication.service_id and raised:

  sqlalchemy.exc.IntegrityError: ForeignKeyViolation on
  topologyserviceapplication — topologyservice.id still referenced

The fix deletes the TopologyServiceApplication join-table rows first,
in the same transaction as the dependency and service deletes, using the
same filter pattern already used for TopologyServiceDependency.

Closes keephq#5439

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 29, 2026

@chengyixu is attempting to deploy a commit to the KeepHQ Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 29, 2026
@dosubot dosubot bot added the Bug Something isn't working label Mar 29, 2026
@chengyixu
Copy link
Copy Markdown
Author

Hi team! This fixes the ForeignKeyViolation crash in process_topology (#5439). All CI checks passing. Happy to address any feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: argocd topology pull error

1 participant