From ac82640c6c722a23620f9096dfc9cd6d235cf2c1 Mon Sep 17 00:00:00 2001 From: eeintech Date: Mon, 12 Oct 2020 17:51:48 -0500 Subject: [PATCH 1/3] Company: allowed duplicate names, made email field unique, custom migration --- .../migrations/0024_auto_20201012_2238.py | 30 +++++++++++++++++++ InvenTree/company/models.py | 5 ++-- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 InvenTree/company/migrations/0024_auto_20201012_2238.py diff --git a/InvenTree/company/migrations/0024_auto_20201012_2238.py b/InvenTree/company/migrations/0024_auto_20201012_2238.py new file mode 100644 index 0000000000..8066ea960d --- /dev/null +++ b/InvenTree/company/migrations/0024_auto_20201012_2238.py @@ -0,0 +1,30 @@ +from django.db import migrations, models + + +def make_empty_email_field_null(apps, schema_editor): + Company = apps.get_model('company', 'Company') + for company in Company.objects.all(): + if company.email == '': + company.email = None + company.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('company', '0023_auto_20200808_0715'), + ] + + operations = [ + migrations.RunPython(make_empty_email_field_null), + 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'), + ), + migrations.AlterField( + model_name='company', + name='name', + field=models.CharField(help_text='Company name', max_length=100, verbose_name='Company name'), + ), + ] diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 2a8d907b76..dd3d170a89 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -82,7 +82,7 @@ class Company(models.Model): class Meta: ordering = ['name', ] - name = models.CharField(max_length=100, blank=False, unique=True, + name = models.CharField(max_length=100, blank=False, help_text=_('Company name'), verbose_name=_('Company name')) @@ -98,7 +98,8 @@ class Company(models.Model): verbose_name=_('Phone number'), blank=True, help_text=_('Contact phone number')) - email = models.EmailField(blank=True, verbose_name=_('Email'), help_text=_('Contact email address')) + email = models.EmailField(blank=True, null=True, unique=True, + verbose_name=_('Email'), help_text=_('Contact email address')) contact = models.CharField(max_length=100, verbose_name=_('Contact'), From 70a3b7f8913cb35f81513ee778c92cf684af2c04 Mon Sep 17 00:00:00 2001 From: eeintech Date: Tue, 13 Oct 2020 07:43:57 -0500 Subject: [PATCH 2/3] Improved migration, still fails if email duplicates exist in current DB --- .../company/migrations/0024_auto_20201012_2238.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/InvenTree/company/migrations/0024_auto_20201012_2238.py b/InvenTree/company/migrations/0024_auto_20201012_2238.py index 8066ea960d..32ca981e3b 100644 --- a/InvenTree/company/migrations/0024_auto_20201012_2238.py +++ b/InvenTree/company/migrations/0024_auto_20201012_2238.py @@ -6,7 +6,7 @@ def make_empty_email_field_null(apps, schema_editor): for company in Company.objects.all(): if company.email == '': company.email = None - company.save() + company.save() class Migration(migrations.Migration): @@ -16,12 +16,21 @@ class Migration(migrations.Migration): ] operations = [ + # Allow email field to be NULL + migrations.AlterField( + model_name='company', + name='email', + field=models.EmailField(blank=True, help_text='Contact email address', max_length=254, null=True, unique=False, verbose_name='Email'), + ), + # 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', From 5793839a0137b344670241269f050fe41b09eff5 Mon Sep 17 00:00:00 2001 From: eeintech Date: Tue, 20 Oct 2020 07:37:07 -0500 Subject: [PATCH 3/3] 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,