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=True
it 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_year
and anemail
has to be unique. That means that there can't be two users where both thefiscal_year
and theemail
are the same (unless these two users are the same user). But since we already madefiscal_year
as a field unique, we already know that this can not happen.You can mark the
email
field asunique=True
as well. This is not equivalent tounique_together = ['fiscal_number', 'email']
. Withunique_together
, the combination of the two columns should be unique, whereas settingunique=True
on multiple fields, means that allUser
s have a differentfiscal_year
, and a differentemail
, etc.