Can a field which is already declared as unique can be used with another field and make a constraint unique together?

Viewed 27

This is my model:

class User(AbstractUser):
    """User model."""

    username = None
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
    email = models.EmailField(_('email address'), unique=True)
    domain=models.CharField(max_length=100)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['email', 'domain'], name='unique_userID')
        ]

Will this work as the email field is already unique??

1 Answers

[W]ill this work as [the] email field is already unique?

Yes, but it makes no sense: since the email is already unique, the combination of domain and email will always be unique anyway. So any combination with the email field is unique, and the UniqueConstraint here thus will not add any extra restriction.

Related