In my Django site I created a custom user model. I am using windows authentication, so up until now, a user was able to go directly to my site. I could also create users in the admin page and edit their permissions there as well. It seems that all of a sudden I am getting the error Violation of UNIQUE KEY constraint 'UQ__accounts__AB6E616413786680'. Cannot insert duplicate key in object 'dbo.accounts_ouser'. The duplicate key value is ().
The error seems like it is on the database side, because I cannot even insert a new ouser even using the server management studio.
class OUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
username = models.CharField(max_length=150, unique=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False) # a superuser
first_name = models.CharField(max_length=100, blank=True, default='')
last_name = models.CharField(max_length=100, blank=True, default='')
date_joined = models.DateField(auto_now=True)
password = models.CharField(max_length=100)
REQUIRED_FIELDS = [] # Email & Password are required by default.
USERNAME_FIELD = 'username'
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
Another weird thing is the use of the UNIQUE KEY constraint, as the column id should be a PRIMARY KEY constraint and the ouser model does not implement any other keys. (I am not sure if that is common or not, but it seems odd to me)
What I would like is for the site to go back to how it was, where a user could visit it if they were authenticated on windows.
Does anybody have any idea why this might be?
The error indicates that you have users violating a unique constraint, and that the unique constraint is a NULL value or an empty string. The two fields with a unique constraint are username
and email
.
Through comments, we checked, and the unique constraint UQ__accounts__AB6E616413786680
actually applied to the email
field. The issue was that email fields were being put in with an empty string value, ''
. Cheers!