From a38835022a63645e8d0dda51bdab85364bbc373c Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 22 Jun 2021 09:33:07 +1000 Subject: [PATCH] Better management of unique constraint for Part --- .../part/migrations/0068_part_unique_part.py | 17 ++++++++++++++ InvenTree/part/models.py | 22 +++---------------- 2 files changed, 20 insertions(+), 19 deletions(-) create mode 100644 InvenTree/part/migrations/0068_part_unique_part.py diff --git a/InvenTree/part/migrations/0068_part_unique_part.py b/InvenTree/part/migrations/0068_part_unique_part.py new file mode 100644 index 0000000000..2e87fc7b2a --- /dev/null +++ b/InvenTree/part/migrations/0068_part_unique_part.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.4 on 2021-06-21 23:10 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0067_partinternalpricebreak'), + ] + + operations = [ + migrations.AddConstraint( + model_name='part', + constraint=models.UniqueConstraint(fields=('name', 'IPN', 'revision'), name='unique_part'), + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 8aa370a7ae..43e54baf91 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -321,6 +321,9 @@ class Part(MPTTModel): verbose_name = _("Part") verbose_name_plural = _("Parts") ordering = ['name', ] + constraints = [ + UniqueConstraint(fields=['name', 'IPN', 'revision'], name='unique_part') + ] class MPTTMeta: # For legacy reasons the 'variant_of' field is used to indicate the MPTT parent @@ -642,23 +645,6 @@ class Part(MPTTModel): 'IPN': _('Duplicate IPN not allowed in part settings'), }) - # Part name uniqueness should be case insensitive - try: - parts = Part.objects.exclude(id=self.id).filter( - name__iexact=self.name, - IPN__iexact=self.IPN, - revision__iexact=self.revision) - - if parts.exists(): - msg = _("Part must be unique for name, IPN and revision") - raise ValidationError({ - "name": msg, - "IPN": msg, - "revision": msg, - }) - except Part.DoesNotExist: - pass - def clean(self): """ Perform cleaning operations for the Part model @@ -671,8 +657,6 @@ class Part(MPTTModel): super().clean() - self.validate_unique() - if self.trackable: for part in self.get_used_in().all():