From 5793839a0137b344670241269f050fe41b09eff5 Mon Sep 17 00:00:00 2001 From: eeintech Date: Tue, 20 Oct 2020 07:37:07 -0500 Subject: [PATCH] Added UniqueConstraint on name/email pair, renamed migration file --- ...2_2238.py => 0024_unique_name_email_constraint.py} | 11 +++++------ InvenTree/company/models.py | 7 +++++-- 2 files changed, 10 insertions(+), 8 deletions(-) rename InvenTree/company/migrations/{0024_auto_20201012_2238.py => 0024_unique_name_email_constraint.py} (82%) diff --git a/InvenTree/company/migrations/0024_auto_20201012_2238.py b/InvenTree/company/migrations/0024_unique_name_email_constraint.py similarity index 82% rename from InvenTree/company/migrations/0024_auto_20201012_2238.py rename to InvenTree/company/migrations/0024_unique_name_email_constraint.py index 32ca981e3b..3a8781f98d 100644 --- a/InvenTree/company/migrations/0024_auto_20201012_2238.py +++ b/InvenTree/company/migrations/0024_unique_name_email_constraint.py @@ -24,16 +24,15 @@ class Migration(migrations.Migration): ), # Convert empty email string to NULL migrations.RunPython(make_empty_email_field_null), - # Make email field unique - migrations.AlterField( - model_name='company', - name='email', - field=models.EmailField(blank=True, help_text='Contact email address', max_length=254, null=True, unique=True, verbose_name='Email'), - ), # Remove unique constraint on name field migrations.AlterField( model_name='company', name='name', field=models.CharField(help_text='Company name', max_length=100, verbose_name='Company name'), ), + # Add unique constraint on name/email pair + migrations.AddConstraint( + model_name='company', + constraint=models.UniqueConstraint(fields=('name', 'email'), name='unique_name_email_pair'), + ), ] diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 829a6fa998..b9fed2ee7b 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -12,7 +12,7 @@ import math from django.utils.translation import gettext_lazy as _ from django.core.validators import MinValueValidator from django.db import models -from django.db.models import Sum, Q +from django.db.models import Sum, Q, UniqueConstraint from django.apps import apps from django.urls import reverse @@ -81,6 +81,9 @@ class Company(models.Model): class Meta: ordering = ['name', ] + constraints = [ + UniqueConstraint(fields=['name', 'email'], name='unique_name_email_pair') + ] name = models.CharField(max_length=100, blank=False, help_text=_('Company name'), @@ -98,7 +101,7 @@ class Company(models.Model): verbose_name=_('Phone number'), blank=True, help_text=_('Contact phone number')) - email = models.EmailField(blank=True, null=True, unique=True, + email = models.EmailField(blank=True, null=True, verbose_name=_('Email'), help_text=_('Contact email address')) contact = models.CharField(max_length=100,