Skip to content

Commit

Permalink
Prevent an incident from having relations with itself
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Aug 19, 2021
1 parent ed099ad commit 56a2251
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2021-08-19 09:19

from django.db import migrations, models
import django.db.models.expressions


class Migration(migrations.Migration):

dependencies = [
('argus_incident', '0003_incident_level'),
]

operations = [
migrations.AddConstraint(
model_name='incidentrelation',
constraint=models.CheckConstraint(check=models.Q(('incident1', django.db.models.expressions.F('incident2')), _negated=True), name='incidentrelation_prevent_loop'),
),
]
7 changes: 7 additions & 0 deletions src/argus/incident/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ class IncidentRelation(models.Model):
type = models.ForeignKey(to=IncidentRelationType, on_delete=models.PROTECT, related_name="incident_relations")
description = models.TextField(blank=True)

class Meta:
constraints = [
models.CheckConstraint(
check=~models.Q(incident1=models.F("incident2")), name="incidentrelation_prevent_loop"
),
]

def __str__(self):
return f"Incident #{self.incident1.pk} {self.type} #{self.incident2.pk}"

Expand Down

0 comments on commit 56a2251

Please sign in to comment.