Reputation: 11
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.
Upvotes: 1
Views: 102
Reputation: 477804
Basically, I would like to know if there is any conflict using the constrain unique_together as well as the parameter
unique=true
in one of the fields that belong to the array of theunique_together
.
It makes no sense. Indeed, by setting unique=True
it means that there can't be two users with the same fiscal_number
, unless if these two users are the same user.
The unique_together = ['fiscal_year', 'email']
means that the combination of a fiscal_year
and an email
has to be unique. That means that there can't be two users where both the fiscal_year
and the email
are the same (unless these two users are the same user). But since we already made fiscal_year
as a field unique, we already know that this can not happen.
You can mark the email
field as unique=True
as well. This is not equivalent to unique_together = ['fiscal_number', 'email']
. With unique_together
, the combination of the two columns should be unique, whereas setting unique=True
on multiple fields, means that all User
s have a different fiscal_year
, and a different email
, etc.
Upvotes: 0