Added UniqueConstraint on name/email pair, renamed migration file

This commit is contained in:
eeintech 2020-10-20 07:37:07 -05:00
parent 7bc925d016
commit 5793839a01
2 changed files with 10 additions and 8 deletions

View File

@ -24,16 +24,15 @@ class Migration(migrations.Migration):
), ),
# Convert empty email string to NULL # Convert empty email string to NULL
migrations.RunPython(make_empty_email_field_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 # Remove unique constraint on name field
migrations.AlterField( migrations.AlterField(
model_name='company', model_name='company',
name='name', name='name',
field=models.CharField(help_text='Company name', max_length=100, verbose_name='Company 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'),
),
] ]

View File

@ -12,7 +12,7 @@ import math
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.db import models 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.apps import apps
from django.urls import reverse from django.urls import reverse
@ -81,6 +81,9 @@ class Company(models.Model):
class Meta: class Meta:
ordering = ['name', ] ordering = ['name', ]
constraints = [
UniqueConstraint(fields=['name', 'email'], name='unique_name_email_pair')
]
name = models.CharField(max_length=100, blank=False, name = models.CharField(max_length=100, blank=False,
help_text=_('Company name'), help_text=_('Company name'),
@ -98,7 +101,7 @@ class Company(models.Model):
verbose_name=_('Phone number'), verbose_name=_('Phone number'),
blank=True, help_text=_('Contact 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')) verbose_name=_('Email'), help_text=_('Contact email address'))
contact = models.CharField(max_length=100, contact = models.CharField(max_length=100,