fix(topology): persist incident before adding alerts (fixes #5463)#6174
Merged
shahargl merged 1 commit intokeephq:mainfrom Mar 30, 2026
Conversation
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
|
@asheeshgupta-devops is attempting to deploy a commit to the KeepHQ Team on Vercel. A member of the Team first needs to authorize it. |
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. 🙌 For any support, feel free to reach out on the community: https://slack.keephq.dev. Happy coding! 👩💻👨💻 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
ForeignKeyViolationwhen creating topology-based incidents in the topology processor.Root Cause
In
_create_application_based_incident(), theIncidentobject was created in memory but not persisted to the database before callingassign_alert_to_incident(). This causedLastAlertToIncidentinserts to fail with:The Fix
Add
session.add(incident)andsession.flush()after creating theIncidentobject to ensure theincident.idexists in the database before any alert associations are created.Changes
keep/topologies/topology_processor.py- Addsession.add()andsession.flush()before alert assignmentTesting
Fixes #5463
Related to #4816