From 21fd6af0f9c36196b0ac12b662847984bd4a456e Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Wed, 31 Jan 2024 15:59:44 -0500 Subject: [PATCH] InstanceLink unique constraint source and target Prevent creating InstanceLinks with duplicate source and target pairings. Signed-off-by: Seth Foster --- awx/main/migrations/0189_inbound_hop_nodes.py | 6 ++++++ awx/main/models/ha.py | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/awx/main/migrations/0189_inbound_hop_nodes.py b/awx/main/migrations/0189_inbound_hop_nodes.py index d23f8a8663..aaaaff9aec 100644 --- a/awx/main/migrations/0189_inbound_hop_nodes.py +++ b/awx/main/migrations/0189_inbound_hop_nodes.py @@ -141,4 +141,10 @@ class Migration(migrations.Migration): help_text='The target receptor address of this peer link.', on_delete=django.db.models.deletion.CASCADE, to='main.receptoraddress' ), ), + migrations.AddConstraint( + model_name='instancelink', + constraint=models.UniqueConstraint( + fields=('source', 'target'), name='unique_source_target', violation_error_message='Field source and target must be unique together.' + ), + ), ] diff --git a/awx/main/models/ha.py b/awx/main/models/ha.py index fe8fbfa0ab..5c1f5df810 100644 --- a/awx/main/models/ha.py +++ b/awx/main/models/ha.py @@ -67,6 +67,14 @@ class HasPolicyEditsMixin(HasEditsMixin): class InstanceLink(BaseModel): class Meta: ordering = ("id",) + # add constraint for source and target to be unique together + constraints = [ + models.UniqueConstraint( + fields=["source", "target"], + name="unique_source_target", + violation_error_message=_("Field source and target must be unique together."), + ) + ] source = models.ForeignKey('Instance', on_delete=models.CASCADE, help_text=_("The source instance of this peer link.")) target = models.ForeignKey('ReceptorAddress', on_delete=models.CASCADE, help_text=_("The target receptor address of this peer link."))