I've added UniqueConstraint into the constraints array in the Meta class which is a subclass of User table.
class User(AbstractBaseUser):
...
class Meta:
verbose_name = _("account")
verbose_name_plural = _("accounts")
indexes = [
models.Index(name="email_index", fields=["email"])
]
constraints = [
models.CheckConstraint(name="check_birth_date", check=Q(birth_date__lt=f"{datetime.date.today()}")),
models.CheckConstraint(name="check_phone_number", check=Q(phone_number__regex=r"^09(1[0-9]|2[0-9]|3[0-9])[0-9]{3}[0-9]{4}$")),
models.UniqueConstraint(name="username_email_unique", fields=["username", "email"])
]
My expectation was that prevent user to enter username and email with same value, But actually did not work and user can do it without getting the IntegrityError Moreover, I'm using Postgresql