Skip to content

fix(topology): persist incident before adding alerts (fixes #5463)#6174

Merged
shahargl merged 1 commit intokeephq:mainfrom
asheeshgupta-devops:fix/topology-processor-foreign-key-violation
Mar 30, 2026
Merged

fix(topology): persist incident before adding alerts (fixes #5463)#6174
shahargl merged 1 commit intokeephq:mainfrom
asheeshgupta-devops:fix/topology-processor-foreign-key-violation

Conversation

@asheeshgupta-devops
Copy link
Copy Markdown
Contributor

Summary

Fixes ForeignKeyViolation when creating topology-based incidents in the topology processor.

Root Cause

In _create_application_based_incident(), the Incident object was created in memory but not persisted to the database before calling assign_alert_to_incident(). This caused LastAlertToIncident inserts to fail with:

psycopg2.errors.ForeignKeyViolation: insert or update on table "lastalerttoincident" 
violates foreign key constraint "lastalerttoincident_incident_id_fkey"
DETAIL: Key (incident_id)=(...) is not present in table "incident".

The Fix

Add session.add(incident) and session.flush() after creating the Incident object to ensure the incident.id exists in the database before any alert associations are created.

# Before (broken):
incident = Incident(...)
for alert in alerts:
    assign_alert_to_incident(...)  # FAILS - incident.id doesn't exist

# After (fixed):
incident = Incident(...)
session.add(incident)
session.flush()  # Now incident.id exists in DB
for alert in alerts:
    assign_alert_to_incident(...)  # Works!

Changes

  • keep/topologies/topology_processor.py - Add session.add() and session.flush() before alert assignment

Testing

Fixes #5463
Related to #4816

Fixes ForeignKeyViolation when creating topology-based incidents.

The issue was that the Incident object was created in memory but
not persisted to the database before calling assign_alert_to_incident().
This caused LastAlertToIncident inserts to fail with:

  ForeignKeyViolation: insert or update on table "lastalerttoincident"
  violates foreign key constraint "lastalerttoincident_incident_id_fkey"
  DETAIL: Key (incident_id)=(...) is not present in table "incident".

The fix adds session.add(incident) and session.flush() after creating
the Incident object to ensure the incident.id exists in the database
before any alert associations are created.

Fixes keephq#5463
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 30, 2026

@asheeshgupta-devops 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:XS This PR changes 0-9 lines, ignoring generated files. label Mar 30, 2026
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 30, 2026

CLA assistant check
All committers have signed the CLA.

@dosubot dosubot bot added the Bug Something isn't working label Mar 30, 2026
Copy link
Copy Markdown
Member

@shahargl shahargl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 30, 2026
@shahargl shahargl merged commit 7239e31 into keephq:main Mar 30, 2026
13 of 14 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

💪 Fantastic work @asheeshgupta-devops! Your very first PR to keep has been merged! 🎉🥳

You've just taken your first step into open-source, and we couldn't be happier to have you onboard. 🙌
If you're feeling adventurous, why not dive into another issue and keep contributing? The community would love to see more from you! 🚀

For any support, feel free to reach out on the community: https://slack.keephq.dev. Happy coding! 👩‍💻👨‍💻

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

Labels

Bug Something isn't working lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: Topology Correlation not working

3 participants