I would like to know if there is any conflict using the constraint unique_together as well as the parameter unique=true in one of the fields that belong to the array of the unique_together.
I can´t remove the parameter unique=true of my field because it´s used as a the foreign key in another model.
class User(AbstractUser):
# User information
fiscal_number = models.CharField(max_length=30,unique=True)
phone = models.CharField(max_length=15, null=True)
email = models.EmailField(max_length=254, verbose_name='email address')
class Meta:
unique_together = ['fiscal_number', 'email']
As you can see the goal is to make the combination of the fields fiscal_number and email unique.
It makes no sense. Indeed, by setting
unique=Trueit means that there can't be two users with the samefiscal_number, unless if these two users are the same user.The
unique_together = ['fiscal_year', 'email']means that the combination of afiscal_yearand anemailhas to be unique. That means that there can't be two users where both thefiscal_yearand theemailare the same (unless these two users are the same user). But since we already madefiscal_yearas a field unique, we already know that this can not happen.You can mark the
emailfield asunique=Trueas well. This is not equivalent tounique_together = ['fiscal_number', 'email']. Withunique_together, the combination of the two columns should be unique, whereas settingunique=Trueon multiple fields, means that allUsers have a differentfiscal_year, and a differentemail, etc.